Getting Uri Null in capture photo from camera intent in samsung mobile

后端 未结 3 1601
情歌与酒
情歌与酒 2021-01-03 12:35

I am getting null in contenturi in samsung phones while capturing photo from camera but rest of others phones its working fine.

@Override 
    protected void         


        
3条回答
  •  青春惊慌失措
    2021-01-03 13:12

    When we will capture the image from Camera in Android then Uri or data.getdata() comes null. We have two solutions to resolve this issue.

    1. We can got the Uri path from the Bitmap Image
    2. We can got the Uri path from cursor.

    I will implement all methods here, Please carefully watch and read these:-

    First I will tell how to get URI from the Bitmap Image: Complete code is:

    First we will capture image through Intent that will same for both methods so this code I will write one time only here:

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

    Now we will Implement OnActivityResult :- (This will be same for both above two methods):-

    @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();
            }
        }
    

    \now we will create all above method to create Uri from Image and Cursor methods via classes:

    Now URI path from Bitmap Image

      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 from Real path of saved image

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

提交回复
热议问题