Android content providers refresh the image and Arraylist sorting,

Source: Internet
Author: User

Android content providers refresh the image and Arraylist sorting,

I moved my previous image selector to our company's project and found numerous pitfalls. It was hard to fill it all the way, but it was almost done in the end, the code hasn't been passed in. The next article is about connecting. First, write the title to be mentioned.

1. I have never understood why content providers can become one of the four major components (there is a reason why my technology is so delicious !!), I understand it now

Whether you delete an image or a new image, you must notify the Android system through the content provider. Otherwise

If you do not want to add an image, the result is that the captured image cannot be found in the mobile album, and the image cannot be found through the content provider, but you can go to the path you set to see it, it's quiet !!!

If you do not want to delete the image, you can find the address of the deleted image through the content provider, but there is no image in the address !!!

How to notify

Add Image

MediaScannerConnection. scanFile (this, new String [] {absolute path of the image}, null, null );

Here we will talk about the 2nd pitfalls. Many articles on Baidu are written and inserted to the System Image Library first.

The code is for the sauce.

try {                MediaStore.Images.Media.insertImage(context.getContentResolver(), output.getAbsolutePath(),                        output.getName(), null);            } catch (FileNotFoundException e) {                e.printStackTrace();            }            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, imageUri));

Where is the pitfall? First, I saved the image to the path I specified, and then the above method was called. Then the point is, there is an image in the album and in my specified path !!!

Second, in the lower-version mobile phone (the simulator I used), the content provider finds two identical pictures. What about the higher-version mobile phone, only the one in the album is found. If the picture is deleted from the album, the image in the specified path is left in the cell phone, wasting memory !!!

In short, I personally think the above method is a little pitfall.

Delete images

Private void DeleteImage (String imgPath) {ContentResolver resolver = context. getContentResolver (); Cursor cursor = MediaStore. images. media. query (resolver, MediaStore. images. media. EXTERNAL_CONTENT_URI, new String [] {MediaStore. images. media. _ ID}, MediaStore. images. media. DATA + "=? ", New String [] {imgPath}, null); boolean result = false; if (cursor. moveToFirst () {long id = cursor. getLong (0); Uri contentUri = MediaStore. images. media. EXTERNAL_CONTENT_URI; Uri uri = ContentUris. withAppendedId (contentUri, id); int count = context. getContentResolver (). delete (uri, null, null); result = count = 1;} else {File file = new File (imgPath); result = file. delete ();} if (result) {Toast. makeText (context, "deleted successfully", Toast. LENGTH_LONG ). show ();}}

3. Arrange the pictures in the order of modification (in chronological order) (before the last shot)

Because all the photos are saved in the list as beans, the Sorting Problem of list is simply put.

First obtain the image modification time

New Date (file. lastModified (). getTime () // file is the image

Then there is a Sort method that stunned me (java is too difficult)

Let the bean class implement the Compartor interface and then implement the compare method.

@Override    public int compare(PhotoBean o1, PhotoBean o2) {         if(o1.getTime() > o2.getTime()){                 return -1;        }else{            return 1;        }    }

This sorting method does not have much to explain, Baidu is good, but I only know this sorting today, so I was shocked to stay !!

Sort directly

Collections. sort (list, new bean class );

All right, so we can sort the Arraylist.

4. Finally, let's take a look at the effects on the simulator. The simulator does not have a camera, but does not affect the effect.

 

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.