即使文件存在于手机上,MediaScannerConnection.scanFile()也不会在Windows资源管理器中显示文件

时间:2016-04-06 09:24:20

标签: android android-mediascanner

方法createNewFile()返回true并显示手机上文件的位置。该文件显示在我手机的文件管理器中,但不在资源管理器中。

基本上我在File对象上调用createNewFile():

File file = new File(path);
try {
        file.createNewFile());

我写信给文件然后打电话:

MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);

这应该在Windows资源管理器中显示,并且这已经发生在另一个应用程序,但它没有与此应用程序,我没有看到任何相关的差异。此外,断开和重新连接USB电缆或重新启动手机都不会导致文件显示在资源管理器中。

1 个答案:

答案 0 :(得分:5)

试试这个:

public static void broadCastToMediaScanner(Context context, File file) {

    Uri contentUri = Uri.fromFile(file);
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);
}