繁体   English   中英

从图库图像路径中选择图像时,android中不返回

[英]while selecting image from gallery image path is not returned in android

嗨,我要从模拟器的图库中选择图像。 当我单击浏览按钮并选择图像时,我已经编写了用于检索图像路径的代码。 但是不显示。 当我使用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);
}

尝试这个。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM