The android system obtains the URI using the absolute path of the image.

Source: Internet
Author: User

In a recent project, you need to find the URI of the image through the absolute path of the image, delete the image, and summarize the method for obtaining the URI.

Android 4.1.3 systems are available.

 

1. Retrieve all image paths, traverse and compare to find the desired path, and retrieve the URI, which is less efficient.

The MediaStore. MediaColumns. DATA Field stores the absolute path of the image,

At last, mImageUri obtains the image URI.


1 Uri mUri = Uri. parse ("content: // media/external/images/media ");
2 Uri mImageUri = null;
3 Cursor cursor = managedQuery (
4 MediaStore. Images. Media. EXTERNAL_CONTENT_URI, null,
5 MediaStore. Images. Media. DEFAULT_SORT_ORDER );
6 cursor. moveToFirst ();
7
8 while (! Cursor. isAfterLast ()){
9 String data = cursor. getString (cursor
10. getColumnIndex (MediaStore. MediaColumns. DATA ));
11 if (picPath. equals (data )){
12 int ringtoneID = cursor. getInt (cursor
13. getColumnIndex (MediaStore. MediaColumns. _ ID ));
14 mImageUri = Uri. withAppendedPath (mUri, "" + ringtoneID );
15 break;
16}
17 cursor. moveToNext ();
18}

2. Retrieve corresponding records directly from the Media Database Based on fields, which is more efficient


1 // TYLT: add by duanyf 20121027 start
2 String type = Utils. ensureNotNull (intent. getType ());
3 Log. d (TAG, "uri is" + uri );
4 if (uri. getScheme (). equals ("file") & (type. contains ("image /"))){
5 String path = uri. getEncodedPath ();
6 Log. d (TAG, "path1 is" + path );
7 if (path! = Null ){
8 path = Uri. decode (path );
9 Log. d (TAG, "path2 is" + path );
10 ContentResolver cr = this. getContentResolver ();
11 StringBuffer buff = new StringBuffer ();
12 buff. append ("(")
13. append (Images. ImageColumns. DATA)
14. append ("= ")
15. append ("'" + path + "'")
16. append (")");
17 Cursor cur = cr. query (
18 Images. Media. EXTERNAL_CONTENT_URI,
19 new String [] {Images. ImageColumns. _ ID },
20 buff. toString (), null, null );
21 int index = 0;
22 for (cur. moveToFirst ();! Cur. isAfterLast (); cur
23. moveToNext ()){
24 index = cur. getColumnIndex (Images. ImageColumns. _ ID );
25 // set _ id value
26 index = cur. getInt (index );
27}
28 if (index = 0 ){
29 // do nothing
30} else {
31 Uri uri_temp = Uri
32. parse ("content: // media/external/images/media /"
33 + index );
34 Log. d (TAG, "uri_temp is" + uri_temp );
35 if (uri_temp! = Null ){
36 uri = uri_temp;
37}
38}
39}
40}
41 // TYLT: add by duanyf 20121027 end

3. Directly delete the image using the delete () method of ContentProvider Based on the path. The two lines of code are done with the highest efficiency.

1 String params [] = new String [] {filepath };
2 ctx. getContentResolver (). delete (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, MediaStore. Images. Media. DATA + "LIKE? ", Params );

PS: You can delete the information and thumbnails of a media repository by using URI without any trace. Deleting a file directly through a path is not clean.

The first two methods get the URI starting with content and a URI starting with file. I don't know the difference between the two methods!

 

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.