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
- if (data = = null) {
- Return
- }
- uri = data.getdata ();
- uri = geturi (data);//Solution
- string[] proj = {MediaStore.Images.Media.DATA};
- Cursor cursor = managedquery (URI, proj, NULL, NULL, NULL);
- if (cursor!=null) {
- int column_index = cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
- Cursor.movetofirst ();
- String path = cursor.getstring (column_index);//The path of the picture in
- Intent intent3 = new Intent (this, syclipactivity.class);
- Intent3.putextra ("path", path);
- Startactivityforresult (Intent3, image_complete);
- }
[HTML]View PlainCopy
- /**
- * Solve the condition that the picture path is null on Xiaomi phone
- * @param Intent
- * @return
- */
- Public Uri GetURI (android.content.Intent Intent) {
- Uri uri = intent.getdata ();
- String type = intent.gettype ();
- if (Uri.getscheme (). Equals ("File") && (Type.contains ("image/"))) {
- String Path = Uri.getencodedpath ();
- if (path! = null) {
- Path = uri.decode (path);
- Contentresolver cr = this.getcontentresolver ();
- StringBuffer buff = new StringBuffer ();
- Buff.append ("("). Append (Images.ImageColumns.DATA). Append ("=")
- . Append ("'" + Path + "'"). Append (")");
- Cursor cur = cr.query (Images.Media.EXTERNAL_CONTENT_URI,
- New string[] {images.imagecolumns._id},
- Buff.tostring (), NULL, NULL);
- int index = 0;
- For (Cur.movetofirst ();!cur.isafterlast (); Cur.movetonext ()) {
- index = cur.getcolumnindex (images.imagecolumns._id);
- Set _id value
- index = cur.getint (index);
- }
- if (index = = 0) {
- Do nothing
- } else {
- Uri uri_temp = uri
- . Parse ("content://media/external/images/media/"
- + index);
- if (uri_temp! = null) {
- uri = uri_temp;
- }
- }
- }
- }
- return URI;
- }
Under this record,
Android solve on Xiaomi phone select photo path null case