I was really drunk today with a broken place.
At first I wrote this:
/** photo */private void Takephoto () { String sdstate = Environment.getexternalstoragestate (); If the SD card can read and write if (Sdstate.equals (environment.media_mounted)) { new File (Imgdir). Mkdirs (); File File = new file (imgfile); Intent Intent = new Intent (mediastore.action_image_capture); Fragment.startactivityforresult (Intent, take_request); } else { mytoast.show (activity, "Please confirm that the SD card has been inserted");} }
/** from album * /private void Getalbum () { Intent Intent = new Intent (Intent.action_pick); Intent.settype ("image/*"); Fragment.startactivityforresult (Intent, pick_request); }
It's simple, isn't it? Yes, Android has been going on for so many years, it's all over the web, and then just the final copy:
public void Onactivityresult (int requestcode, int resultcode, Intent data, Myuri URI) {if (ResultCode! = activity. RESULT_OK) {return;} if (Requestcode = = pick_request) {if (data.getdata () = null) {Uri.geturi (Data.getdata ());}} else if (Requestcode = = Take_ REQUEST) {if (data.getdata () = null) {Uri.geturi (Data.getdata ());}}}
I ran very well on the Samsung S5, but I had to change my cell phone to the big God cell phone and the Nubian Z7. Debugging for a while, found data = = null.
So when data = = is null, I go to fetch Data.getparcelableextra ("data"), not null, just like the previous method.
Well, the Nubian phone can get pictures of the photos to be cropped. However, the picture is very vague, according to the internet is here to return to the thumbnail image pictures, so to solve this problem, or it will definitely suffer. So we use the original image to cut the line, so changed a little bit of code:
/** Photo */ private void Takephoto () { String sdstate = Environment.getexternalstoragestate (); If the SD card can read and write if (Sdstate.equals (environment.media_mounted)) { new File (Imgdir). Mkdirs (); File File = new file (imgfile); Intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile (file)); Fragment.startactivityforresult (Intent, take_request); } else { mytoast.show (activity, "Please confirm that the SD card has been inserted");} }
According to the Android design, said a little if it is to understand the image of the photo to save the address, then the returned data is null. (But I still write a non-null condition, although the great God F2 in this still will jump, but there is no particularly good way to ...) )
The code that accepts the callback is also handled accordingly:
public void Onactivityresult (int requestcode, int resultcode, Intent data, Myuri URI) {if (ResultCode! = Activity . RESULT_OK) {return; } if (Requestcode = = pick_request) {if (data.getdata () = null) {Uri.geturi (data.getdat A ()); }} else if (Requestcode = = take_request) {if (data! = null) {Uri Myuri; if (data.getdata () = null) {Uri.geturi (Data.getdata ()); Return } if (Data.hasextra ("data")) {Bitmap Bitmap = Data.getparcelableextra ("Data"); try {Myuri = Uri.parse (MediaStore.Images.Media.insertImage (activity.getcontentresolver (), Bitmap, NULL, and null)); Uri.geturi (Myuri); } catch (Exception e) {e.printstacktrace (); }}} or else { try {Bitmap Bitmap = Bitmapfactory.decodefile (imgfile); if (bitmap = = null) {return; } String s = MediaStore.Images.Media.insertImage (Activity.getcontentresolver (), bitmap, NULL, NULL); Uri Myuri = Uri.parse (s); Uri.geturi (Myuri); } catch (Exception e) {e.printstacktrace (); } } } }
I tried the Nubian z7,htc M9, Huawei Glory 6 Plus, Xiaomi 2, Xiaomi 4, are OK, but with the big God F2, on the throw null pointer abnormal. And it's always been this sentence:
String s = MediaStore.Images.Media.insertImage (Activity.getcontentresolver (), bitmap, NULL, NULL);
Bitmap there no matter what to pass, Bitmap, string These 2 parameters are not, and I looked at the source code, in fact, the final conversion to Bitmap this parameter. The location of the anomaly in the source code, grandma, labor tangle for a long time.
The great God F2, as long as a tune above that piece of code jumped off. Since this is not the way to go, I see the file has a way to directly convert the URI, so the final code is:
public void Onactivityresult (int requestcode, int resultcode, Intent data, Myuri URI) {if (ResultCode! = activity. RESULT_OK) {return; } if (Requestcode = = pick_request) {if (data.getdata () = null) {Uri.geturi (Data.getdata ()); }} else if (Requestcode = = take_request) {if (data! = null) {Uri Myuri; if (data.getdata () = null) {Uri.geturi (Data.getdata ()); Return } if (Data.hasextra ("data")) {Bitmap Bitmap = Data.getparcelableextra ("Data"); try {Myuri = Uri.parse (MediaStore.Images.Media.insertImage (Activity.getcontentresolver (), Bitmap, null , null)); Uri.geturi (Myuri); } catch (Exception e) {e.printstacktrace (); }}} else {file file = new file (imgfile); if (file = null && file.exists ()) { Uri.geturi (uri.fromfile (file)); } } }}
Basically here, the great God F2 can be normal to get pictures of photos to cut. Troubled the Labor Day, what is called again, some roads you do not go to walk, never know what is going on the road, although it is just a way you feel very easy.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android calls photo album problems encountered