从画廊图像路径中选择图像时不会在android中返回

时间:2014-06-20 08:48:28

标签: android image

您好我在模拟器中从图库中选择图像。当我单击浏览按钮并选择图像时,我编写了代码来检索图像的路径。但它没有显示出来。当我使用Log在logcat中打印路径时,它会显示错误。请帮我看看图像路径。这是我的代码。


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
           logo_path.setText(selectedImagePath);

        }
    }
}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

2 个答案:

答案 0 :(得分:0)

试试这个。

public String getImagePathFromURI(Uri uri) {

    String imgpath = "";
    Cursor c= getContentResolver().query(uri, null, null, null, null);

    if (c == null) { 

        imgpath= uri.getPath();  // Getting path from url itself

    } else { 

        c.moveToFirst();
        int id = c.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        result = c.getString(id);
        c.close();  // Close curson.
    }

    return imgpath;  // original image path
}

答案 1 :(得分:0)

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    if (cursor == null)
        return null;
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

并在onActivityResult使用

String path = getRealPathFromURI(uri); 
Bitmap bmp = BitmapFactory.decodeFile(path);