意向在图片上返回null

意向在图片上返回null

本文介绍了意向在图片上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

         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);

基本上onactivityresult就是我的阅读方式,并且我选择的图像为null.当我从文件管理器(/sdcard)选择图像时..但是,当我从uhf播放器中选择图像时..我从相机或屏幕截图中选择图片,效果很好

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);

推荐答案

//Here is some sample code to pick photo from gallery or get from camera.

//声明以下内容

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

//调用方式-结果从图库(SD卡)中选择照片

//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方法

//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"/>

这篇关于意向在图片上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:04