Android ACTION_MEDIA_SCANNER_SCAN_FILE直到重启才显示图像?

Mon*_*lue 7 android android-intent android-mediascanner

我目前正在开发一个Android应用程序,其中用户拍摄存储在设备上的外部存储器中的照片,然后我也试图让图库扫描文件以将新媒体添加到图库但是这似乎不会更新直到设备重启.

我使用的代码如下:

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Log.v("INFO", mCurrentPhotoPath.toString());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
Run Code Online (Sandbox Code Playgroud)

使用我的应用程序拍照后,我可以使用设备上的文件管理器应用程序验证它是否存在,并且在将文件传递给Media Scanner之前记录它看起来是正确的文件路径

file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Run Code Online (Sandbox Code Playgroud)

谢谢亚伦

Mon*_*lue 2

我设法解决了这个问题。该问题是由于我设置 mCurrentPhotoPath 的位置造成的。所以我更新了代码来使用

photoFile = createImageFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)