Get selected pictures in Android and get photos return results difference

Source: Internet
Author: User

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

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.