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

Que*_*ner 3 android android-mediascanner

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

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

File file = new File(path);
try {
        file.createNewFile());
Run Code Online (Sandbox Code Playgroud)

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

MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
Run Code Online (Sandbox Code Playgroud)

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

Say*_*yem 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);
}
Run Code Online (Sandbox Code Playgroud)