Android solve on Xiaomi phone select photo path null case

Source: Internet
Author: User

Yesterday, the test guy said his cell phone selection library collapsed, this is an upload avatar function, I believe that many applications have this function, so I put the phone to play log to see the next return path is NULL, search the next solution on the Internet, now the solution record:

This is performed in the Onactivityresult method,

[HTML]View PlainCopy
  1. if (data = = null) {
  2. Return
  3. }
  4. uri = data.getdata ();
  5. uri = geturi (data);//Solution
  6. string[] proj = {MediaStore.Images.Media.DATA};
  7. Cursor cursor = managedquery (URI, proj, NULL, NULL, NULL);
  8. if (cursor!=null) {
  9. int column_index = cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
  10. Cursor.movetofirst ();
  11. String path = cursor.getstring (column_index);//The path of the picture in
  12. Intent intent3 = new Intent (this, syclipactivity.class);
  13. Intent3.putextra ("path", path);
  14. Startactivityforresult (Intent3, image_complete);
  15. }

[HTML]View PlainCopy
  1. /**
  2. * Solve the condition that the picture path is null on Xiaomi phone
  3. * @param Intent
  4. * @return
  5. */
  6. Public Uri GetURI (android.content.Intent Intent) {
  7. Uri uri = intent.getdata ();
  8. String type = intent.gettype ();
  9. if (Uri.getscheme (). Equals ("File") && (Type.contains ("image/"))) {
  10. String Path = Uri.getencodedpath ();
  11. if (path! = null) {
  12. Path = uri.decode (path);
  13. Contentresolver cr = this.getcontentresolver ();
  14. StringBuffer buff = new StringBuffer ();
  15. Buff.append ("("). Append (Images.ImageColumns.DATA). Append ("=")
  16. . Append ("'" + Path + "'"). Append (")");
  17. Cursor cur = cr.query (Images.Media.EXTERNAL_CONTENT_URI,
  18. New string[] {images.imagecolumns._id},
  19. Buff.tostring (), NULL, NULL);
  20. int index = 0;
  21. For (Cur.movetofirst ();!cur.isafterlast (); Cur.movetonext ()) {
  22. index = cur.getcolumnindex (images.imagecolumns._id);
  23. Set _id value
  24. index = cur.getint (index);
  25. }
  26. if (index = = 0) {
  27. Do nothing
  28. } else {
  29. Uri uri_temp = uri
  30. . Parse ("content://media/external/images/media/"
  31. + index);
  32. if (uri_temp! = null) {
  33. uri = uri_temp;
  34. }
  35. }
  36. }
  37. }
  38. return URI;
  39. }


Under this record,

Android solve on Xiaomi phone select photo path null case

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.