在库中的ACTION_GET_CONTENT在ImageView的setImageURI()中不起作用后,Uri返回

Eig*_*ght 12 android uri imageview

我从画廊使用Uri获取图像

Intent intent = new Intent();  
intent.setType("image/*");  
intent.setAction(Intent.ACTION_GET_CONTENT);  
startActivityForResult(Intent.createChooser(intent, "Choose Picture"), requestCode);
Run Code Online (Sandbox Code Playgroud)

并尝试显示图像

imageView.setImageURI(uri);
Run Code Online (Sandbox Code Playgroud)

这里,uri是intent.getData()在onActivityResult中收到的图像的Uri.

但没有显示图像.另外,为

File file=new File( uri.getPath() );
Run Code Online (Sandbox Code Playgroud)

file.exists()返回false.

Moh*_*ikh 27

问题是你得到了Uri,但是从那个uri你必须创建Bitmap以在你的Imageview中显示.这个代码有各种各样的机制来做同样的事情.

Intent intent = new Intent();  
intent.setType("image/*");  
intent.setAction(Intent.ACTION_GET_CONTENT);  
startActivityForResult(Intent.createChooser(intent, "Choose Picture"), 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if(resultCode==RESULT_CANCELED)
    {
        // action cancelled
    }
    if(resultCode==RESULT_OK)
    {
        Uri selectedimg = data.getData();
        imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg));
    }
}
Run Code Online (Sandbox Code Playgroud)