Before I encountered a problem, I found that after I generated a new Bitmap in my program, I did not see the newly generated image when I opened the Gallery view of the system. However, open the file browser and find the folder where you saved the Bitmap, and you can see the Bitmap file generated by the program. That is, the file actually exists, but Gallery does not seem to refresh. Then another new behavior confirms my view that the newly generated Bitmap will be displayed in the Gallery after each reboot of the device. In other words, after restarting the device, the system re-read and write a file that holds the picture information (that is, the conclusion is that the specific principle will be devoted to blog discussion).
Well, the principle can guess a ballpark, then guess the method: Maybe you can notify the system to save the picture information of the file to refresh, do a scan of the folder. But is there any way to do that? With doubt, I looked for information on the Internet, found that the principle is really so, the method is also some.
This address : http://www.cnblogs.com/rossoneri/p/4239152.html
There are now two ways to solve this problem:
Solution 1: Send the broadcast to notify the system to refresh.
Sendbroadcast (new Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ())));
However, you need to add permissions to the program using this method:
<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <android:name= "Android.permission.RESTART_PACKAGES"/>
This method I did not test, but look at the introduction should be possible.
Solution 2: Using mediascannerconnection:
First, inherit the mediascannerconnectionclient for the class that will use the method:
Public class YourView implements mediascannerconnectionclient {}
Inheriting the class requires overriding two methods:
@Override Public void onmediascannerconnected () { // TODO auto-generated method stub try { "image/jpg"); Catch (Exception e) { e.printstacktrace (); }} @Overridepublicvoid onscancompleted (String path, Uri uri) { // TODO auto-generated method stub Msc.disconnect ();}
method to see its name can be guessed, the former is connected on the Mediascanner after the search file. Bitmappath is the absolute path to the file, "Image/jpg" is the file format, JPG can be changed to PNG or even *. The latter method is to disconnect after the lookup has ended.
Finally, create the Mediascannerconnection object and call its method after the picture is generated:
mediascannerconnection MSC; .. if NULL ) { new mediascannerconnection (mactivity, ThisClass. this); Msc.connect ();
This way, the next time you create a new picture, you can see the newly created picture in the Gallery!
Well, the problem is solved. Then I found a problem: if I deleted a picture in the program, and found that the path of the picture in Gallery, can see the picture, but the open picture is a gray. Say the simple point, is that I use the code to delete the image file, the system's saving information or the path of the file exists, only restart will delete these invalid paths. Okay, here's the question, how do you change it?
Can you do it again like the previous steps? The previous steps seem to be refreshing a configuration file for the system, and doing it again might be fine. After trying to find out (the first method didn't try.) ), however, it does not work.
Since this is not possible, it may be that I do not understand some of the principles of the system, there should be other methods. After a few explorations, I found a solution, and I got a better idea of how the Android system scanned the information stored. In the next article, we will discuss this principle and solution in a simple way. Let's get here today.
Resources:
[Android Instance] The bitmap cannot be seen in the gallery after saving, but the file is present
Image, saved to sdcard, doesn ' t appear in Android ' s Gallery app
[Android] processing that is not displayed on the system gallery for generated picture files