Retrieve image files based on image Uri

Source: Internet
Author: User

1. Obtain the image file based on the Contact Image Uri and display it on ImageView. The Code is as follows:

 Uri uri = Uri.parse("content://com.android.contacts/display_photo/1"               afd = getContentResolver().openAssetFileDescriptor(uri, "r"         [] buffer =  [16 * 1024         FileInputStream fis =                  FileOutputStream fos =  FileOutputStream( File("sdcard/11212"                  ByteArrayOutputStream temp_byte =                    ((size = fis.read(buffer)) != -1             fos.write(buffer, 0             temp_byte.write(buffer, 0                   image.setImageBitmap(BitmapFactory.decodeByteArray(temp_byte.toByteArray(), 0     }       }       }

As you can see above, uri is the photo_uri field in the view_data view of the contact database. The final id should be adjusted according to the actual situation.

2. Obtain the resource storage path based on mediaURI

 Cursor cur = getContentResolver().query(Uri.parse("content://media/external/images/media/279"), , , ,   (cur !=       ( i = 0; i < cur.getColumnCount(); i++          Log.d("Davds", "name = " +                        Log.d("Davds", "" + cur.getString(cur.getColumnIndex("_data"  }

The value obtained from the filed corresponding to _ data is the file storage path.

The following figure is displayed in the database (images table in the MediaProvider database ):

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.