Android redmi solves the problem of reading images from local folders and obtaining paths,

Source: Internet
Author: User

Android redmi solves the problem of reading images from local folders and obtaining paths,

In Android development, you can read images from local folders. Use the following code to open the image selection list:

Intent intent = new Intent();  intent.setAction(Intent.ACTION_PICK);  intent.setType("image/*");  startActivityForResult(intent, RESULT_LOAD_IMAGE);  

After selecting an image, in onActivityResult, we use the following code to obtain the selected image path:

Uri uri = data.getData();  String path = uri.getPath(); 

In this Code, I tested and found that there was no problem in testing on my Nexus 4, meizu, and simulator, But I tested it on red rice, Huawei, and other mobile phones, the obtained path value is not the path of the image on the mobile phone. We do not know what the value is. If we get the image based on this path, we will get a null value, why ???

Solution: In onActivityResult, you can use it on all mobile phones in the following way without any problems:

If (data! = Null) {Uri uri = data. getData (); if (! TextUtils. isEmpty (uri. getAuthority () {Cursor cursor = getContentResolver (). query (uri, new String [] {MediaStore. images. media. DATA}, null); if (null = cursor) {Toast. makeText (this, "image not found", Toast. LENGTH_SHORT ). show (); return;} cursor. moveToFirst (); path = cursor. getString (cursor. getColumnIndex (MediaStore. images. media. DATA); cursor. close ();} else {path = uri. getPath () ;}} else {Toast. makeText (this, "image not found", Toast. LENGTH_SHORT ). show (); return ;}

  

  

 

 

  

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.