Phonegap 3.3.0 pass downloaded file cdvfile url to

2019-08-12 05:53发布

I managed to download a file to the local storage but I can't manage to pass it's path to the media scanner so it gets added to the gallery.

I've added a plugin that does the media scanner work. The function that tries to do the trick is this one.

private boolean mediaScanner(String absolutePath, CallbackContext callbackContext)  throws InterruptedException, JSONException
{
      Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      String path = Uri.parse(absolutePath).getPath();
      File f = new File(path.substring(path.indexOf('/', 1)));
      mediaScanIntent.setData(Uri.fromFile(f));
      this.cordova.getActivity().sendBroadcast(mediaScanIntent);
      return true;
}

The absolute path parameter is the file.toURL() from the javascript. It's an url that looks like this. cdvfile://localhost/persistent/Download/13republica_personal.jpg

I tried to modify it to /Download/13republica_personal.jpg but it doesn't work.

How should I pass the file url/location to the media scanner so the file is added to the gallery?

1条回答
戒情不戒烟
2楼-- · 2019-08-12 06:11

Ended up using the last developer version of phonegap file plugin and using toLocalURL method of the file object as parameter of the media scanner.

查看更多
登录 后发表回答