在Android中插入图像后,如何刷新图库?

时间:2010-11-10 13:17:52

标签: android

我已经使用android API在插件库中添加了如下内容:

  

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

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

有没有办法按需刷新图库?

由于

Bassel Kh。

5 个答案:

答案 0 :(得分:27)

我最近遇到了这个问题,我尝试了@ 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

答案 1 :(得分:24)

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

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

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

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

此解决方案更快,因为图库无法完全重新扫描。但不是那么值得信赖,因为有关该文件的所有信息都是手动添加的。

答案 2 :(得分:3)

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

答案 3 :(得分:2)

您可以使用它来刷新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 :(得分:0)

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

Sheets("UI").Range("A1").End(xlDown).Select
i = Selection.Row + 1

然后

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

newFIleName是一个文件路径,您可以将其获取为

refreshGallery(newFileName);
相关问题