0

I am trying to use Biokys crop Image and it says you need pass it a file path of an image.

I have a URI that I'm passing to it:

    intent = getActivity().getIntent();
    uri = intent.getParcelableExtra("Image");

    Intent intent = new Intent(getActivity(), CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, uri.getPath());
    intent.putExtra(CropImage.SCALE, true);
    intent.putExtra(CropImage.ASPECT_X, 3);
    intent.putExtra(CropImage.ASPECT_Y, 2);

    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);

Here is the onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != getActivity().RESULT_OK) {

        return;
    }

    Bitmap bitmap;

    switch (requestCode) {



    case REQUEST_CODE_CROP_IMAGE:

        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        if (path == null) {

            return;
        }

        Picasso.with(getActivity()).load(path).into(mImageView);
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

But when I try to pass my Uri to the crop class I get the following in my Logcat:

file /external/images/media/39690 not found

Would anyone know what I can do to get it to read my Uri? thanks

Jack
  • 2,023
  • 7
  • 37
  • 68

1 Answers1

1

You could use the Uri to obtain document id, and then query either MediaStore.Images.Media.EXTERNAL_CONTENT_URI or MediaStore.Images.Media.INTERNAL_CONTENT_URI (depending on the SD card situation).

To get document id:

String wholeID = DocumentsContract.getDocumentId(uriThatYouCurrentlyHave);

// Split at colon, use second item in the array
String id = wholeID.split(":")[1];

String[] column = { MediaStore.Images.Media.DATA };     

// where id is equal to             
String sel = MediaStore.Images.Media._ID + "=?";

Cursor cursor = getContentResolver().
                          query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                          column, sel, new String[]{ id }, null);

String filePath = "";

int columnIndex = cursor.getColumnIndex(column[0]);

if (cursor.moveToFirst()) {
    filePath = cursor.getString(columnIndex);
}   

cursor.close();

Reference: https://stackoverflow.com/a/20413475/3326331

Community
  • 1
  • 1
Sagar Pilkhwal
  • 5,914
  • 2
  • 27
  • 78