19

因此,在我的应用程序中,我有一次将一堆图像保存到一个临时文件夹中,并且我希望它们立即显示在图库中。重新启动后,他们会这样做,但否则他们不会。

我试过使用 sendBroadcast 方法:

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
     Uri.parse("file://" + Environment.getExternalStorageDirectory())));

但我得到一个权限错误:

E/AndroidRuntime( 2628): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2628, uid=10068

我是否会在我的 AndroidManifest 中缺少权限,或者这不再受支持?谢谢

4

8 回答 8

21

Petrus 在另一个答案中提供的代码适用于 Kitkat(4.4):

MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
 *   (non-Javadoc)
 * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
 */
public void onScanCompleted(String path, Uri uri) 
  {
      Log.i("ExternalStorage", "Scanned " + path + ":");
      Log.i("ExternalStorage", "-> uri=" + uri);
  }
    });
于 2013-11-08T19:53:31.420 回答
9

我试过Environment.getExternalStorageDirectory().toString()了,但它没有找到我的自定义文件夹。

我只是想分享我解决这个问题的经验。

最后,我MediaScannerConnection配置为一次扫描一个文件,现在出现了保存这些图像的丢失文件夹。每次下载每个图像时,我都会将MediaScannerConnection文件路径称为 Uri 而不是文件夹本身。

此外,许多人问为什么sendBroadcast停止在 kitkat 上工作,答案是sendBroadcast被滥用和调用太多次并导致系统耗尽电池或减速,因此他们删除了直接调用以防止滥用,这是有道理的。我希望将上述解决方案用于保存所有图像的文件夹,但它不适用于该文件夹。我希望查找文件夹会暴露自定义文件夹中的其余文件,但在我的情况下并没有。希望以后能找到更好的答案……

这是我的代码片段。

MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
              new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                  Log.i(TAG, "Scanned " + path);
                }
              });
于 2014-01-24T02:24:05.960 回答
7

试试这个:将文件保存到文件夹后,编写以下代码

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
于 2016-09-12T10:59:42.530 回答
5

下面的代码适用于保存图像后刷新图库的所有设备。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
       Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
       File f1 = new File("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
       Uri contentUri = Uri.fromFile(f1);
       mediaScanIntent.setData(contentUri);
       sendBroadcast(mediaScanIntent);
   } else {
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
   }
   sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
于 2016-08-31T05:36:13.350 回答
2

实现MediaScannerConnectionClient并添加以下代码。在 4.4 中完美运行 :)

MediaScannerConnection conn;

public void startScan(String url) {
    imagepath = url;
    if (conn != null)
        conn.disconnect();
    conn = new MediaScannerConnection(activity.this, activity.this);
    conn.connect();
}

@Override
public void onMediaScannerConnected() {
    try {
        conn.scanFile(imagepath, getMimeType(imagepath));
    } catch (java.lang.IllegalStateException e) {
        //Do something
    }
}

@Override
public void onScanCompleted(String path, Uri uri) {
    conn.disconnect();
}
于 2014-01-13T06:23:09.947 回答
2

I know its late but try this can working in all versions:

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    Intent mediaScanIntent = new Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                } else {
                    sendBroadcast(new Intent(
                            Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                }
于 2015-02-11T07:21:09.617 回答
0

使用 MediaScannerConnection 而不是 SendBroadcast..

MediaScannerConnection.MediaScannerConnectionClient(
{
    @Override
    public void onScanCompleted(String path, Uri uri) {

        if (path.equals(**your filename**.getAbsolutePath()))
        {
            Log.i("Scan Status", "Completed");
            Log.i("uri: ",uri.toString());

            conn.disconnect();
        }
    }
    @Override
    public void onMediaScannerConnected()
    {
        // TODO Auto-generated method stub
        conn.scanFile(**your file name**.getAbsolutePath(),null);
    }
});

conn.connect();
于 2014-08-13T09:05:23.047 回答
-1
 FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
//Gallery Refresh Code         

    MediaScannerConnection.scanFile(getActivity(), new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
                /*
                 *   (non-Javadoc)
                 * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
                 */
                public void onScanCompleted(String path, Uri uri)
                {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });
            out.close();
于 2018-10-18T12:23:33.147 回答