Cannot find file in the gallerie after downloading it with PhoneGap in Android

后端 未结 3 1539
夕颜
夕颜 2021-01-16 18:03

i have a problem with the the downloaded files in my PhoneGap-App for Android. The download-Function from PhoneGap actually works quite well i think. It gets the file from t

3条回答
  •  长情又很酷
    2021-01-16 18:32

    I don't know if that helps you in PhoneGap / JavaScript context:

    Your problem is that the gallery will only display files that are indexed in the device media-database. Just adding a file to the file system will not automatically add it to that database. And pretty much the only time a rescan / update of that database happens is when you reboot the device or remount the sdcard (after it was shared with a PC or in case you ejected it and put it back in).

    To have a file added to the database the simplest way to to that is to send Intent#ACTION_MEDIA_SCANNER_SCAN_FILE to the MediaScanner to let it add your file to the database. Once that is done the file will show up in the gallery.

    Java code for that would be

    File newImage = new File("/mnt/sdcard/Download/google.png");
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    scanIntent.setData(Uri.fromFile(newImage));
    sendBroadcast(scanIntent);
    

提交回复
热议问题