Lead:
Today's Android apps are mostly combined to take pictures and choose from albums (of course, this is the product dog thing, the beginning of the topic). )
Today in the writing of a functional module encountered an embarrassing problem, the same is the photo to get the image function, on the colleague's Samsung run no problem, but on my mobile phone on a run on the system-level modification of the android is too big, even the error log can not come out, And when the boss will give me a test machine, the following is the test on a certain type of phone error results
Error Reporting:
10-21 17:41:43.634:e/androidruntime (32683): FATAL exception:main
10-21 17:41:43.634:e/androidruntime (32683): java.lang.RuntimeException:Failure delivering result resultinfo{who =null, request=10, Result=-1, data=intent {act=inline-data (have extras)} to activity {* * * *}: Java.lang.NullPointerExce Ption
Problem Analysis:
This is the wrong part of the code, you can see I get the URI directly from the return intent, but in fact some phones after taking pictures of the URI is empty, so the null pointer exception is not strange
if(ResultCode = =RESULT_OK) { URI uri = data.getdata (); Contentresolver CR= This. Getcontentresolver (); Try { if(BMP! =NULL) bmp.recycle (); BMP=Bitmapfactory.decodestream (Cr.openinputstream (URI)); BMP=MediaStore.Images.Media.getBitmap (CR, URI); ImageData=Streamutils.stream2bytearray (cr. Openinputstream (URI)); Avfile Img_file=NewAvfile ("Photo_1", ImageData); Img_file.saveinbackground (); Images.add (Img_file); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } photo.setimagebitmap (BMP); } Else{toast.maketext (publishactivity). This, "Please re-select the picture", Toast.length_short). Show (); }
So the solution is to handle the two cases of selecting and taking pictures separately, so as to be able to adapt to more devices
Solutions
10 represents a photo, and 20 represents a selection from the album.
if(Requestcode = = 10) {if(ResultCode = =RESULT_OK) { if(BMP! =NULL) bmp.recycle (); BMP= (Bitmap) Data.getextras (). Get ("Data")); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Bmp.compress (Bitmap.CompressFormat.PNG,100, BAOs); ImageData=Baos.tobytearray (); Avfile Img_file=NewAvfile ("Photo_1", ImageData); Img_file.saveinbackground (); Images.add (Img_file); Photo.setimagebitmap (BMP); } Else{toast.maketext (publishactivity). This, "Please re-select the picture", Toast.length_short). Show (); } } if(Requestcode = = 20) { if(ResultCode = =RESULT_OK) {URI Uri=Data.getdata (); Contentresolver CR= This. Getcontentresolver (); Try { if(BMP! =NULL) bmp.recycle (); BMP=Bitmapfactory.decodestream (Cr.openinputstream (URI)); BMP=MediaStore.Images.Media.getBitmap (CR, URI); ImageData=Streamutils.stream2bytearray (cr. Openinputstream (URI)); Avfile Img_file=NewAvfile ("Photo_1", ImageData); Img_file.saveinbackground (); Images.add (Img_file); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } photo.setimagebitmap (BMP); } Else{toast.maketext (publishactivity). This, "Please re-select the picture", Toast.length_short). Show (); } }
Get selected pictures in Android and get photos return results difference