onActivityResult 返回 Intent data.getData(); always Null Only in Marshmallow 和 Lollipop

onActivityResult returning Intent data.getData(); always Null Only in Marshmallow and Lollipop

阅读我的全部代码。除了 MarshmallowLollipop 之外,它在每个 Phone 上都能完美运行。在 MarshmallowLollipop 手机中 唯一的问题是 data.getData() 仅在 Picture Captured 的情况下返回 null。如果我捕获 Video 它工作正常。我有 4 个案例 -

  1. 正在从图库中选择图片并在 Imageview 中显示其缩略图 - 工作正常。

  2. 正在从相机捕获图片并在 Imageview 中显示其缩略图 - MarshmallowLollipop 中不工作

  3. 正在从图库中选择视频并在 Imageview 中显示其缩略图 - 工作正常。

  4. 3.Capturing 来自相机的视频并在 Imageview 中显示其缩略图 - 工作正常。

现在有很多解决方案,但它们都不令人满意,如果代码在视频捕获的情况下工作正常,那么为什么它不能用于图像捕获?这是一个 Bug 还是我哪里做错了?

这是我的代码。我评论了发生崩溃的一行-

 @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      int THUMBSIZE = 120;
      switch (requestCode){


        case RESULT_LOAD_IMAGE:


            if (resultCode==RESULT_OK){
                String column_Name= MediaStore.Images.Media.DATA;
                String picturePath=getPath(column_Name,data.getData());
                Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath, BitmapFactory.decodeFile(picturePath));
                Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap, THUMBSIZE, THUMBSIZE));
                thumbnailview.setBackground(fullpic);
                editText.setText(picturePath);
                picker.setVisibility(View.VISIBLE);
                thumbnailview.setClickable(false);

            }
            break;

        case REQUEST_IMAGE_CAPTURE:

            if (resultCode==RESULT_OK){
                String picturePath="";
                String column_Name=MediaStore.Images.Media.DATA;

                picturePath=getPath(column_Name,data.getData());

                    //My app Crashes here because in Marshmallow data.getData() is always null.

                Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath, BitmapFactory.decodeFile(picturePath));
                Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap, THUMBSIZE, THUMBSIZE));
                thumbnailview.setBackground(fullpic);
                editText.setText(picturePath);
                picker.setVisibility(View.VISIBLE);
                thumbnailview.setClickable(false);


            }
            break;

        case RESULT_LOAD_VIDEO:

            if (resultCode==RESULT_OK){
                String column_Name=MediaStore.Video.Media.DATA;
                String videoPath=getPath(column_Name,data.getData());
                Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND));
                thumbnailview.setBackground(fullpic);
                editText.setText(videoPath);
                picker.setVisibility(View.VISIBLE);
                thumbnailview.setClickable(false);


            }
            break;

        case REQUEST_VIDEO_CAPTURE:

            if (resultCode==RESULT_OK){
                String column_Name=MediaStore.Video.Media.DATA;
                String videoPath=getPath(column_Name,data.getData());
                Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND));
                thumbnailview.setBackground(fullpic);
                editText.setText(videoPath);
                picker.setVisibility(View.VISIBLE);
                thumbnailview.setClickable(false);


            }
            break;
    }

    if (picker != null) {
        picker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OpenDialog();
            }
        });
    }

}


private String getPath(String column_Name,Uri uri){

    String[] projection = {column_Name};
    String path = "";
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index_data ;
    if (cursor != null) {
        column_index_data = cursor.getColumnIndexOrThrow(column_Name);
        cursor.moveToFirst();
        path = cursor.getString(column_index_data);
        cursor.close();
    }

    return path;
}

private void OpenDialog(){
    dialogBox.setTitle("Select an Action");
    dialogBox.setMessage("Choose Picture or Video");

    dialogBox.setPositiveButton("Picture", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (check_Permissions()){
                OpenCameraDialog();
            }
            else {
                request_Permissions();
                CAMERA_DIALOG_PERMISSION=1;

            }
        }
    });

    dialogBox.setNegativeButton("Video", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (check_Permissions()){
                OpenVideoDialog();
            }
            else {
                request_Permissions();
                VIDEO_DIALOG_PERMISSION=1;
            }

        }
    });

    dialogBox.show();

}



private void OpenCameraDialog(){

    dialogBox.setTitle("Select an Action");
    dialogBox.setMessage("Choose Picture From");

    dialogBox.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            final Intent galleryintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryintent, RESULT_LOAD_IMAGE);
        }
    });

    dialogBox.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

        }
    });

    dialogBox.show();

}

private void OpenVideoDialog(){

    dialogBox.setTitle("Select an Action");
    dialogBox.setMessage("Choose Video From");

    dialogBox.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            final Intent galleryintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryintent, RESULT_LOAD_VIDEO);
        }
    });

    dialogBox.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            final Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

            if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            }
        }
    });
    dialogBox.show();

}

private boolean check_Permissions(){

    boolean GRANTED;

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) +
            ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) +
            ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) +
            ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
        GRANTED=false;

    }
    else {
        GRANTED=true;
    }
    return GRANTED;
}

private void request_Permissions(){
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO},
            REQUEST_FOR_PERMISSION);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){

        case REQUEST_FOR_PERMISSION:

            if ((grantResults.length>0)&& (grantResults[0] +grantResults[1]+grantResults[2]+grantResults[3]== PackageManager.PERMISSION_GRANTED)){

                if (CAMERA_DIALOG_PERMISSION==1){
                    OpenCameraDialog();
                    CAMERA_DIALOG_PERMISSION=0;
                }
                else if (VIDEO_DIALOG_PERMISSION==1){
                    OpenVideoDialog();
                    VIDEO_DIALOG_PERMISSION=0;
                }

            }
            else {
                Toast.makeText(this, "Please GRANT Permissions", Toast.LENGTH_SHORT).show();
            }

    }

}

It is working perfectly on every Phone except Marshmallow and Lollipop

不,不是。当用户选择编写良好的相机应用程序来处理您的 ACTION_IMAGE_CAPTURE 请求时,它会在许多 Android 版本上失败。

你的问题出在这里:

            if (data.getData()!=null){
            picturePath=getPath(column_Name,data.getData());}
            else {
                //My app Crashes here because in Marshmallow data.getData() is always null.
            }

这里至少有两个缺陷。

最重要的是假设您从 ACTION_IMAGE_CAPTURE 得到 UriThat is not documented,相机应用不需要 return 一个 Uri。特别是,在您的结构中,您只会通过 getExtra("data") 获得缩略图。如果您想要全尺寸图像,请在 Intent 上使用 EXTRA_OUTPUT,在这种情况下,您知道图像的存储位置 — 它就是您在 EXTRA_OUTPUT 中指示的任何位置。

另一个是您假设您返回的 Uri 来自 MediaStore 或者有一个 MediaStore.Video.Media.DATA 列。相机应用不仅不必 return 一个 Uri,而且也不需要 Uri 来自 MediaStore 或具有这样的列。

如@CommonsWare 建议 -

Camera App do not need to return uri.

此外,

You need to tell Camera app where to write image.

所以,我用-

替换了我的代码
 final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            pictureUri=getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

并在 onActivityResult -

 case REQUEST_IMAGE_CAPTURE:

            if (resultCode==RESULT_OK){
                String picturePath="";
                String column_Name= MediaStore.Images.Media.DATA;
                if (data!=null){
                if (data.getData()!=null){
                picturePath=getPathfromUri(column_Name,data.getData());}

                else {
                    picturePath= pictureUri.getPath();

                }
                }
                else {
                    picturePath= pictureUri.getPath();

                }}

当我们在 android 中从相机捕获图像时,Uridata.getdata() 变为空。我们有两种解决方案来解决这个问题。

  1. 我们可以从Bitmap Image中得到Uri路径
  2. 我们可以从游标中得到Uri路径。

我会在这里实现所有方法,请仔细观看和阅读这些:-

首先我将介绍如何从位图图像中获取Uri: 完整代码是:

首先我们将通过 Intent 捕获图像,这对于两种方法都是相同的,所以这段代码我只在这里写一次:

 // Capture Image
        captureImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent, reqcode);
                }

            }
        });

现在我们将实现 OnActivityResult :-(这对于以上两种方法都是相同的):-

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


        if(requestCode==reqcode && resultCode==RESULT_OK)
        {

Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView.setImageBitmap(photo);

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);

            \ Show Uri path based on Image
            Toast.makeText(LiveImage.this,"Here "+ tempUri, Toast.LENGTH_LONG).show();

           \ Show Uri path based on Cursor Content Resolver
            Toast.makeText(this, "Real path for URI : "+getRealPathFromURI(tempUri), Toast.LENGTH_SHORT).show();
}
        else
        {
            Toast.makeText(this, "Failed To Capture Image", Toast.LENGTH_SHORT).show();
        }
    }

\现在我们将创建上述所有方法,通过 类:

从 Image 和 Cursor 方法创建 Uri

现在来自位图图像的 URI 路径

  private Uri getImageUri(Context applicationContext, Bitmap photo) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(LiveImage.this.getContentResolver(), photo, "Title", null);
        return Uri.parse(path);
    }

\ 保存图片真实路径的Uri

  public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }

亲爱的朋友复制并粘贴这段代码,然后尝试理解。 如果您正在制作企业应用程序,我 100% 肯定这对您会更好。 如果您喜欢我的代码,请为我投票....

你可以从 Bitmap 中获取 Uri 所以

private Uri getImageUri(Context applicationContext, Bitmap bitmapImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), bitmapImage, "Some Title", null);
        return Uri.parse(path);
    }



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode==CAPTURE_IMAGE_CODE){

                Bitmap bitmapImage = (Bitmap) data.getExtras().get("data");
                //ImageView.setImageBitmap(photo);

                Uri inputImageUri = getImageUri(getApplicationContext(),bitmapImage); 
            }
        }
    }

并添加 onClick 调用

private void takeImage(){

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, CAPTURE_IMAGE_CODE);
        }

    }