Android photo-saving problem solving method _android in System albums not shown

Source: Internet
Author: User
Maybe everyone knows that when we save the album to the Android phone, then to open the system library can not find the picture we want, that is because we inserted the picture has not been updated, first of all, to explain the way to insert the system library, very simple, a code can be achieved
Copy Code code as follows:

MediaStore.Images.Media.insertImage (Getcontentresolver (), Mbitmap, "", "");

You can insert it into the System gallery by using the code above. At this time there is a problem, that is, we can not specify the name of the inserted photos, but the system gave us a current number of milliseconds for the name, there is a problem for a long time, I still first put the source of the Insertimage
Copy Code code as follows:

/**
* Insert an image and create a thumbnail for it.
*
* @param cr The content resolver to use
* @param source The stream to use for the image
* @param title The name of the image
* @param Description The description of the image
* @return The URL to the newly created image, or <code>null</code> if the image failed to is stored
* For any reason.
*/
public static final String insertimage (contentresolver cr, Bitmap Source,
string title, string description) {
Contentvalues values = new Contentvalues ();
Values.put (Images.Media.TITLE, TITLE);
Values.put (Images.Media.DESCRIPTION, DESCRIPTION);
Values.put (Images.Media.MIME_TYPE, "image/jpeg");
Uri URL = null;
String stringurl = null; /* value to be returned * *
try {
url = Cr.insert (External_content_uri, values);
if (source!= null) {
OutputStream imageout = cr.openoutputstream (URL);
try {
Source.compress (Bitmap.CompressFormat.JPEG, M, imageout);
finally {
Imageout.close ();
}
Long id = contenturis.parseid (URL);
Wait until Mini_kind thumbnail is generated.
Bitmap minithumb = Images.Thumbnails.getThumbnail (CR, ID,
Images.Thumbnails.MINI_KIND, NULL);
This is a for backward compatibility.
Bitmap microthumb = Storethumbnail (CR, Minithumb, ID, 50F, 50F,
Images.Thumbnails.MICRO_KIND);
} else {
LOG.E (TAG, "Failed to create thumbnail, removing original");
Cr.delete (URL, null, NULL);
url = null;
}
catch (Exception e) {
LOG.E (TAG, "Failed to insert Image", e);
if (URL!= null) {
Cr.delete (URL, null, NULL);
url = null;
}
}
if (URL!= null) {
StringUrl = Url.tostring ();
}
return stringurl;
}

The above method has a title, I just thought it is possible to set the name of the picture, set up a bit, the original is not, depressed, which master know the title of this field is why, tell the younger brother, appreciate!
Of course, Android also provides a way to insert a system album, you can specify the name of the save picture, I put the source code posted
Copy Code code as follows:

/**
* Insert an image and create a thumbnail for it.
*
* @param cr The content resolver to use
* @param imagepath The path to the image to insert
* @param name The name of the image
* @param Description The description of the image
* @return The URL to the newly created image
* @throws FileNotFoundException
*/
public static final String insertimage (Contentresolver cr, String ImagePath,
String name, string description) throws FileNotFoundException {
Check if file exists with a FileInputStream
FileInputStream stream = new FileInputStream (ImagePath);
try {
Bitmap BM = bitmapfactory.decodefile (ImagePath);
String ret = Insertimage (CR, BM, name, description);
Bm.recycle ();
return ret;
finally {
try {
Stream.Close ();
catch (IOException e) {
}
}
}

Ah ah, paste the source code I just found that this method calls the first method, this name is the above method of title, Dizzy dead, this more depressed, anyway I set title no effect, ask the master for the younger brother answer, first, we continue to say
The code above is plugged into the system album and needs A clockwork broadcast.
Copy Code code as follows:

Sendbroadcast (New Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ()));

The above broadcast is to scan the entire SD card broadcast, if you have a lot of SD card inside will scan for a long time, in the scan we are unable to access the SD card, so the user reflects very bad, with the micro-letter friends know that the micro-letter to save pictures to the system album did not scan the entire SD card, so we use the following method
Copy Code code as follows:

Intent Intent = new Intent (intent.action_media_scanner_scan_file);
Uri uri = uri.fromfile (New File ("/sdcard/image.jpg"));
Intent.setdata (URI);
Mcontext.sendbroadcast (Intent);

or use Mediascannerconnection.
Copy Code code as follows:

Final mediascannerconnection msc = new Mediascannerconnection (Mcontext, New Mediascannerconnectionclient () {
public void onmediascannerconnected () {
Msc.scanfile ("/sdcard/image.jpg", "image/jpeg");
}
public void onscancompleted (String path, Uri Uri) {
LOG.V (TAG, "scan completed");
Msc.disconnect ();
}
});

You're going to ask me, how do I get the path to the picture we just inserted? Oh, this own method gets, Insertimage (contentresolver cr, Bitmap source,string title, String description), this method returns to us is inserts the picture the URI, We can get the absolute path to the picture based on this URI.
Copy Code code as follows:

Private String Getfilepathbycontentresolver (context context, Uri Uri) {
if (null = = URI) {
return null;
}
Cursor C = context.getcontentresolver (). Query (URI, NULL, NULL, NULL, NULL);
String filePath = null;
if (null = = c) {
throw New IllegalArgumentException (
"Query on" + URI + "returns null result.");
}
try {
if ((C.getcount ()!= 1) | |!c.movetofirst ()) {
} else {
FilePath = c.getstring (
C.getcolumnindexorthrow (Mediacolumns.data));
}
finally {
C.close ();
}
return filePath;
}

According to the above method to obtain is the absolute path of the picture, this way we do not have to send a scan of the entire SD card broadcast, hehe, wrote here is finished, write a very messy, I hope you will look at the hope that you have helped!
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.