从文件显示ImageView找不到此文件

时间:2014-11-18 11:46:37

标签: android

我需要显示文件类型中的图片,这是我的代码段:

//Part 1 how I'm setting my file (picking image from gallery)
    currImageURI = data.getData();
    myImage= new File(currImageURI.getPath());

//Part 2 how I'm trying to show my image from the File
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageURI(Uri.fromFile(myImage));

这是myImage在展示时的价值:

  

路径:/ document / image:19144

这是有效的路径和有效的Image,但是我收到了这个错误:

  

resolveUri在错误的位图uri上失败:file:/// document / image%3A19144

那么这里有什么问题? 我必须使用FILE类型。从文件类型显示图像。

2 个答案:

答案 0 :(得分:0)

尝试:

ImageView.setImageUri(Uri.fromFile(new File("/path/to/image")));

或者用:

ImageView.setImageUri(Uri.parse(new File("/path/to/image").toString()));

更新

Uri imageUri = data.getData();
InputStream imageStream = null;
try {
    imageStream = getContentResolver().openInputStream(imageUri);
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
} catch (FileNotFoundException e) {
    // Handle the error
} finally {
    if (imageStream != null) {
        try {
            imageStream.close();
        } catch (IOException e) {
            // Ignore the exception
        }
    }
}

答案 1 :(得分:-1)

我就是这样做的。尝试一次

Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgProfilePic);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
相关问题