传递URI图像路径

时间:2014-09-11 19:53:20

标签: android uri crop

我正在尝试使用Biokys crop Image,它说你需要传递一个图像的文件路径。

我有一个我要传递给它的URI:

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

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

但是当我尝试将我的Uri传递给crop类时,我在Logcat中得到以下内容:

file /external/images/media/39690 not found

有谁知道我能做什么才能让它读到我的Uri?感谢

1 个答案:

答案 0 :(得分:1)

您可以使用Uri获取document id,然后查询MediaStore.Images.Media.EXTERNAL_CONTENT_URIMediaStore.Images.Media.INTERNAL_CONTENT_URI(取决于SD卡情况)。

获取文档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();

参考:https://stackoverflow.com/a/20413475/3326331