Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

android - intent returns null on picture

         if (resultCode == Activity.RESULT_OK && requestCode == 1
                && null != data)
            {               
            Uri selectedImage = data.getData();
            InputStream imageStream =getActivity().getContentResolver().openInputStream(selectedImage);
            System.out.println("dfsdf");
            Bitmap bitmap2 = BitmapFactory.decodeStream(imageStream);

basically onactivityresult thats how i read, and i get selected image as null. when i selected my image from file manager ( /sdcard)..however when i selected from uhf player..i select from camera or screenshot, it works fine

               Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"), 1);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
//Here is some sample code to pick photo from gallery or get from camera. 

//Declare the following

 private static final int SELECT_PHOTO = 100;
     private static final int CAMERA_REQUEST=101;

//way to call startactivityforresult select photo from gallery(sd card)

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

//way to call startactivityforresult select photo from camera

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         startActivityForResult(cameraIntent, CAMERA_REQUEST);

//onActivityResult method

 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

                if(resultCode == RESULT_OK){  

                //pick image from gallery(sd card)  
                    if(requestCode==SELECT_PHOTO)
                    {

                    Uri selectedImage = imageReturnedIntent.getData();
                    InputStream imageStream = null;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                    imageView_Babypic.setImageBitmap(yourSelectedImage);
                }
                    //pick image from camera
                    else if (requestCode==CAMERA_REQUEST) {
                         Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data"); 
                         imageView_Babypic.setImageBitmap(photo);
                    }


                }

            }

//at last use this for camera use in your Manifest file

 <uses-permission android:name="android.permission.CAMERA"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...