[Android] does not display the generated image files in the system Gallery,

Source: Internet
Author: User

[Android] does not display the generated image files in the system Gallery,

I encountered a problem before, that is, after I generated a new Bitmap in the program, I did not see the new image when I opened the system's Gallery view. However, open the file browser, find the folder where the Bitmap is saved, and you can see the Bitmap file generated by the program. That is to say, the file actually exists, but Gallery does not seem to be refreshed. Then, another new action confirms my point of view, that is, each time the device is restarted, the new Bitmap will be displayed in the Gallery. That is to say, after the device is restarted, the system re-reads and writes a file containing image information. (The conclusion is as follows: I will write a blog post to discuss the specific principles ).

Well, the principle can be used to guess a rough idea. You can guess the method: You may be able to notify the system to refresh the file with image information and perform a scan of the folder. But is there any way? With questions, I searched for information on the Internet and found that the principle was indeed the case, and there were some methods.

Address: http://www.cnblogs.com/rossoneri/p/4239152.html

There are two ways to solve this problem:

 

Solution 1: Send a broadcast to notify the system to refresh.

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

However, to use this method, you need to add permissions to the program:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.RESTART_PACKAGES" />

I have not tested this method, but it should be okay to see the Introduction.

 

Solution 2: Use MediaScannerConnection:

First, the class to use this method inherits MediaScannerConnectionClient:

public class YourView  implements MediaScannerConnectionClient {}

To inherit this class, you must override the following two methods:

@Overridepublic void onMediaScannerConnected() {    // TODO Auto-generated method stub    try {        msc.scanFile(bitmapPath, "image/jpg");    } catch (Exception e) {        e.printStackTrace();    }}@Overridepublic void onScanCompleted(String path, Uri uri) {    // TODO Auto-generated method stub    msc.disconnect();}

The method can be guessed by its name. The former is connected to MediaScanner and then the file is searched. BitmapPath is the absolute path of the file, "image/jpg" is the file format, and jpg can be changed to png or even *. The latter method is to find and disconnect after the end.

Finally, create a MediaScannerConnection object and call the method after generating the image:

MediaScannerConnection msc;.....if (msc != null) {    msc.disconnect();}msc = new MediaScannerConnection(mActivity, thisClass.this);msc.connect();

In this way, after you regenerate a new image, you can see the new image in Gallery!

 

Well, the problem is solved. Then I found another problem: If I deleted an image in the program, I found that there is still a path for this image in Gallery, however, the image is gray. To put it simply, I used code to delete the image file. The path of the file still exists in the system's storage information. These invalid paths will be deleted only after restart. Okay, the problem is coming. How can this problem be solved?

Can I try again like the previous steps? The preceding steps seem to have refreshed a configuration file of the system, and may be done again. After trying, I found that (the first method did not try ..), However, this is not the case.

If this is not the case, it may be because I have not understood some of the principles of the system, and there should be other methods. After some exploration, I found a solution and learned more about how the Android system scans and stores the information. The next article briefly discusses the principles and solutions. Come here today.

 

References:

[Android instance] After saving bitmap, it cannot be seen in the Image Library, but the file exists.

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.