1

I am calling camera intent to capture an image and get the image uri in onActivtyResult in my fragment.

this is my captureImage()

 private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA_STORAGE);
 }

this is my onActivityResult in fragment

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CAMERA_STORAGE && resultCode == RESULT_OK) {
        Uri targetUri = data.getData();
        String imagePath;
        if (data.toString().contains(AppConstants.CONTENT)) {
            imagePath = getRealPathFromURI(targetUri);
        } else if (data.toString().contains(AppConstants.CONTENT)) {
            imagePath = targetUri.getPath();
        } else {
            imagePath = null;
        }
        Log.d("imageuri",imagePath);
    }

this getRealPathFromUri()

 public String getRealPathFromURI(Uri uri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(uri, proj, null, null,
                null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

this works fine in one phone and in some phones, in onActivityResult data.getData() returns null.

to resolve this i came across this Capture Image from Camera and Display in Activity

but here it returns a thumbnail image. So is there any work around to get the captured image uri from camera.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38

3 Answers3

0

Try This

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}
Siddharth Patel
  • 205
  • 1
  • 13
0

Write below method to get real path

public static String getRealPath(Context context, Uri uri) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
        if (isExternalStorageDocument(uri)) {
            String docId = DocumentsContract.getDocumentId(uri);
            String[] split = docId.split(":");
            String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        } else if (isDownloadsDocument(uri)) {

            String id = DocumentsContract.getDocumentId(uri);
            Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        } else if (isMediaDocument(uri)) {
            String docId = DocumentsContract.getDocumentId(uri);
            String[] split = docId.split(":");
            String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            String selection = "_id=?";
            String[] selectionArgs = new String[]{
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

private static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

private static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

private static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

private static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}

private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    String column = "_data";
    String[] projection = {column};

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}
Jyot
  • 540
  • 5
  • 17
  • data is only null so, buy the way i appreciate for the answer – hasan_shaikh Apr 16 '18 at 12:56
  • Using debug can you plz post the real path of the image. It will be something like "com.android.**************" – Jyot Apr 17 '18 at 09:47
  • data is only null in onActivityResult in some phones, So how will i get real path of the image. Buy the way @Mohammed Farhan has answered, which is working for me. – hasan_shaikh Apr 17 '18 at 09:52
0

Try this in onActivityResult()

Uri imageUri = data.getData ();

and then you can set it to imageview

Neha
  • 389
  • 4
  • 8
  • 24