【问题标题】:How can I refresh the Gallery after I inserted an Image in android?在android中插入图像后如何刷新图库?
【发布时间】:2011-05-07 21:18:12
【问题描述】:

我使用 android API 添加了一个插入到图库中,如下所示:

Images.Media.insertImage(ctx.getContentResolver(), "scard/test.jpg", "你好", “描述”);

实际上我通过其完整路径(scard/test.jpg)的图像已经成功插入到数据库中,但是当您打开图库时,您无法看到它,除非您关闭/打开设备或安装/卸载外部存储器。

有什么方法可以按需刷新图库吗?

谢谢

巴塞尔 Kh.

【问题讨论】:

  • 您的方法不起作用,因为您使用的是相对文件路径“scard/test.jpg”而不是绝对文件路径“/scard/test.jpg”。

标签: android


【解决方案1】:

我把所有东西都绑起来了,然后我找到了这个完美的解决方案! 您可以将此方法应用于任何文件类型(jpg、png、pdf 等)

public void refreshGallery(File f) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent mediaScanIntent = new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri fileUri = Uri.fromFile(f); //out is your output file
        mediaScanIntent.setData(fileUri);
        sendBroadcast(mediaScanIntent);
    } else {
        sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    }
}
    

然后

refreshGallery(newFileName);

newFIleName 是一个文件路径,你可以得到你的文件路径

File newFileName = new File(filePath + "/" + fileName + ".jpg");

【讨论】:

    【解决方案2】:

    使用媒体扫描器 (sendBroadcast) 的解决方案。但是,可能在具有大量图片和数据的 sdcard 上,此操作应该会达到很高的处理成本。

    还有另一种解决方案,将媒体文件保存到图库后,您应该通知图库数据库插入了另一个文件。可以这样做:

    private void addImageGallery( File file ) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // or image/png
        getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }
    

    此代码不会将您的媒体文件保存到图库,只会将有关新文件的信息保存在图库数据库中。您应该先存储真实文件。

    此解决方案速度更快,因为不会完全重新扫描图库。但不是那么可信,因为有关文件的所有信息都是手动添加的。

    【讨论】:

      【解决方案3】:

      您可以使用它来刷新 Android 图库:

      public void refreshAndroidGallery(Uri fileUri) {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                  Intent mediaScanIntent = new Intent(
                          Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                  mediaScanIntent.setData(fileUri);
                  mContext.sendBroadcast(mediaScanIntent);
              } else {
                  mContext.sendBroadcast(new Intent(
                          Intent.ACTION_MEDIA_MOUNTED,
                          Uri.parse("file://" + Environment.getExternalStorageDirectory())));
              }
          }
      

      【讨论】:

        【解决方案4】:

        我最近遇到了这个问题,我尝试了@Samuh的解决方案,但直到我从Google's example找到解决方案后才能完美运行:

           // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(this,
                    new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });
        

        我尝试过自己并完美地工作。

        或者,类似地,您可能想查看 MediaScanner Class 的参考资料,而 StackOverflow 上的某个人之前曾问过这个问题: Image, saved to sdcard, doesn't appear in Android's Gallery app

        【讨论】:

          【解决方案5】:
          static public boolean resetExternalStorageMedia(Context context) {
              if (Environment.isExternalStorageEmulated())
                  return (false);
              Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
              Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, uri);
          
              context.sendBroadcast(intent);
              return (true);
          }
          
          static public void notifyMediaScannerService(Context context, String path) {
              MediaScannerConnection.scanFile(context,
                      new String[] { path }, null,
                      new MediaScannerConnection.OnScanCompletedListener() {
                  public void onScanCompleted(String path, Uri uri) {
                      Log.i("ExternalStorage", "Scanned " + path + ":");
                      Log.i("ExternalStorage", "-> uri=" + uri);
                  }
              });
          }
          

          【讨论】:

            猜你喜欢
            • 2013-09-08
            • 2020-11-28
            • 1970-01-01
            • 1970-01-01
            • 2011-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-05
            相关资源
            最近更新 更多