Android:保存新图像后刷新图库

时间:2013-09-04 21:38:02

标签: android gallery mediastore

所以在我的应用程序中,我一度将一堆图像保存到临时文件夹中,我希望它们立即显示在图库中。重新启动,他们这样做,但否则他们没有。

我尝试过使用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中是否可能遗漏了权限,或者这是否已不再支持?感谢

8 个答案:

答案 0 :(得分:20)

Petrus在another answer提供的代码在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);
  }
    });

答案 1 :(得分:7)

我尝试了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);
                }
              });

答案 2 :(得分:5)

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

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));

答案 3 :(得分:2)

实施MediaScannerConnectionClient并在4.4 :)中完美地添加以下code.Works

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();
}

答案 4 :(得分:2)

我知道它已经很晚了,但试试这可以在所有版本中使用:

   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())));
                }

答案 5 :(得分:2)

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

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"))));

答案 6 :(得分: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();

答案 7 :(得分:0)

 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();