保存到 sdcard 的图像不会出现在 Android 的图库应用中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2170214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:22  来源:igfitidea点击:

Image, saved to sdcard, doesn't appear in Android's Gallery app

androidimagesavegallery

提问by Michael Kessler

I save an image to the sdcard and it doesn't appear in the Gallery application until I pull off the sdcard and return it back.

我将图像保存到 sdcard 中,并且在我拔下 sdcard 并将其返回之前,它不会出现在 Gallery 应用程序中。

Do you have any idea why is it so?

你知道为什么会这样吗?

Seems like the Gallery application has some cache that isn't updated on file save...

似乎画廊应用程序有一些缓存不会在文件保存时更新......

Actually, I also want to open the just-saved image in Gallery application and have no success with that
thisis my question about this issue.

实际上,我也想在图库应用程序中打开刚刚保存的图像,但没有成功,
是我关于此问题的问题。

采纳答案by hackbod

The system scans the SD card when it is mounted to find any new image (and other) files. If you are programmatically adding a file, then you can use this class:

系统在挂载 SD 卡时扫描它以查找任何新的图像(和其他)文件。如果您以编程方式添加文件,则可以使用此类:

http://developer.android.com/reference/android/media/MediaScannerConnection.html

http://developer.android.com/reference/android/media/MediaScannerConnection.html

回答by darrenp

A simpler solution is to use the static convenience method scanFile():

一个更简单的解决方案是使用静态便捷方法scanFile()

File imageFile = ...
MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/jpeg" }, null);

where thisis your activity (or whatever context), the mime-type is only necessary if you are using non-standard file extensions and the nullis for the optional callback (which we don't need for such a simple case).

this您的活动(或任何上下文)在哪里,仅当您使用非标准文件扩展名时才需要 mime 类型,并且nullis 用于可选回调(对于这种简单的情况,我们不需要)。

回答by ShadowGod

My answer to the original question and to anyone else that may have this problem:

我对原始问题和其他可能有此问题的人的回答:

I was having this same problem, images in my app that people saved to the SD card were not showing up in their Gallery immediately. After some searching I found this one line of code inserted after my 'save to sdcard' code that fixed the problem:

我遇到了同样的问题,我的应用程序中人们保存到 SD 卡的图像没有立即显示在他们的图库中。经过一番搜索,我发现在解决问题的“保存到 sdcard”代码之后插入了一行代码:

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

回答by Janusz

You can also add an Image to the Media Gallery by intent, have a look at the example code to see how it is done:

您还可以通过意图将图像添加到媒体库,查看示例代码以了解它是如何完成的:

ContentValues image = new ContentValues();

image.put(Images.Media.TITLE, imageTitle);
image.put(Images.Media.DISPLAY_NAME, imageDisplayName);
image.put(Images.Media.DESCRIPTION, imageDescription);
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/png");
image.put(Images.Media.ORIENTATION, 0);

 File parent = imageFile.getParentFile();
 String path = parent.toString().toLowerCase();
 String name = parent.getName().toLowerCase();
 image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
 image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
 image.put(Images.Media.SIZE, imageFile.length());

 image.put(Images.Media.DATA, imageFile.getAbsolutePath());

 Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);

回答by AtanuCSE

Gallery refresh including Android KITKAT

图库刷新,包括 Android KITKAT

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
}
else
{
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}

回答by 3dmg

Here is the code for the MediaScannerConnection:

这是 MediaScannerConnection 的代码:

MyMediaConnectorClient client = new MyMediaConnectorClient(newfile);
MediaScannerConnection scanner = new MediaScannerConnection(context, client);
client.setScanner(scanner);
scanner.connect();

newfile is the File object of your new/saved file.

newfile 是新/保存文件的 File 对象。

回答by Abhishek Susarla

there is an app in the emulator that says - ' Dev Tools'

模拟器中有一个应用程序,上面写着-“开发工具”

click on that and select ' Media Scanning'.. all the images ll get scanned

单击它并选择“媒体扫描”.. 所有图像都将被扫描

回答by TOMKA

Let your activity implement 'MediaScannerConnectionClient' and add this to your activity:

让您的活动实现“MediaScannerConnectionClient”并将其添加到您的活动中:

private void startScan() 
{ 
    if(conn!=null) conn.disconnect();  
    conn = new MediaScannerConnection(YourActivity.this,YourActivity.this); 
    conn.connect(); 
} 

@Override 
public void onMediaScannerConnected() { 
    try{
        conn.scanFile(yourImagePath, "image/*");
       } catch (java.lang.IllegalStateException e){
       }
}

@Override 
public void onScanCompleted(String path, Uri uri) { 
    conn.disconnect(); 
} 

回答by user3245604

this work with me

和我一起工作

File file = ..... // Save file

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

回答by Khyati Fatania

 File folderGIF = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/newgif2");    //path where gif will be stored
success = folderGIF.mkdir();    //make directory
 String finalPath = folderGIF + "/test1.gif";  //path of file
.....
/* changes in gallery app if any changes in done*/
 MediaScannerConnection.scanFile(this,
                    new String[]{finalPath}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });