Android4.4 ContentResolver does not update the ContentResolver after the image is invalid and the image is added to the image library is deleted.

Source: Internet
Author: User

Problem Background:

The reference link allows you to browse images, scan gallery photos with ContentResolver, and display the photos in the listview in strict chronological order. As shown in:


The problem is that 4.2 of mobile phones can be normally displayed, but with the addition of photos, this scan remains unchanged. It's really annoying. Another serious problem is that one image cannot run on 4.4 of mobile phones. Grandpa, it's really a pitfall. It took me several hours to waste, and then I almost gave up. I accidentally browsed my previous blog and found that the real reason was that I lost one permission.



You must have the following two permissions to play a role when sending broadcast updates. In particular, the second permission. If you do not have this permission, you can browse on 4.2 of your mobile phones, but it will not be updated. You cannot scan images at 4.4. But it is strange that no exception is reported in both cases.

The key query statements are as follows:

Uri mImageUri = MediaStore. images. media. EXTERNAL_CONTENT_URI; Log. I ("yanguoqi", "mImageUri =" + mImageUri. getPath (); ContentResolver mContentResolver = MainActivity. this. getContentResolver (); // only query jpeg image Cursor mCursor = mContentResolver. query (mImageUri, null, MediaStore. images. media. MIME_TYPE + "=? ", New String [] {" image/jpeg "}, MediaStore. images. media. DATE_TAKEN); mCursor. moveToLast (); int num = mCursor. getCount (); do {// obtain the image path String path = mCursor. getString (mCursor. getColumnIndex (MediaStore. images. media. DATA); if (path. contains ("/DCIM/Camera") {imgPaths. add (path) ;}while (mCursor. moveToPrevious (); mCursor. close ();

1. mContentResolver. query (mImageUri ...) This uri can only be Uri mImageUri = MediaStore. Images. Media. EXTERNAL_CONTENT_URI; constructed URI. Rather than writing a path to construct a URI, this is really strange;

2. to filter files, you can only filter the files in the Image Library by using the if (path. contains ("/DCIM/Camera") statement;

3. to strictly sort the photos by time, write MediaStore in the query. images. media. DATE_TAKEN is not yet in progress. The first cursor found in this writing is indeed the latest, but it is obtained through List. add operation. Instead, the first operation is arranged to the end. Therefore, I first move the cursor, and the reason why do is in front of while is not to miss the first image.

4. Before each query, you should send a broadcast update:

private void updateGallery(){Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);   //, MediaStore.Images.Media.EXTERNAL_CONTENT_URIString path = Environment.getExternalStorageDirectory() + "/DCIM/Camera"; Uri uri = Uri.fromFile(new File(path));   intent.setData(uri);   this.sendBroadcast(intent);  }

In the broadcast area, you can specify the uri of the path.
 MediaStore.Images.Media.EXTERNAL_CONTENT_URI

.

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.