Search code examples
androidandroid-cameraandroid-camera-intent

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


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 onActivityResult(int requestCode, int resultCode, Intent data) {     
        super.onActivityResult(requestCode, resultCode, data);    
        try
        {
             if (requestCode == IMAGE_CAPTURE) {
                if (resultCode == RESULT_OK){

                    Uri contentUri = data.getData();
                    if(contentUri!=null)
                    {
                        String[] proj = { MediaStore.Images.Media.DATA };         
                        Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
                        cursor.moveToFirst();         
                        imageUri = Uri.parse(cursor.getString(column_index));
                    }

                    tempBitmap = (Bitmap) data.getExtras().get("data"); 
                    mainImageView.setImageBitmap(tempBitmap);
                    isCaptureFromCamera = true;
                }
            }

Solution

  • This above code works in some mobile but does not work in samsung mobile in my case, so I implemented the common logic for all devices.

    After capturing the photo from camera, I implement a logic using Cursor and iterate the cursor to get the path of last captured photo from camera.The below code works fine on any device.

    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
    if(cursor != null && cursor.moveToFirst())
    {
        do {
            uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
            photoPath = uri.toString();
        }while(cursor.moveToNext());
        cursor.close();
    }