android – 从图库的ACTION_GET_CONTENT之后返回的Uri在ImageView的setImageURI()中不起作用

我正在从画廊使用Uri获取图像
Intent intent = new Intent();  
intent.setType("image/*");  
intent.setAction(Intent.ACTION_GET_CONTENT);  
startActivityForResult(Intent.createChooser(intent,"Choose Picture"),requestCode);

并尝试显示图像

imageView.setimageURI(uri);

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

但没有显示图像.另外,

File file=new File( uri.getPath() );

file.exists()返回false.

解决方法

问题是你得到Uri,但是从那个uri你必须创建Bitmap来显示在你的Imageview.这里有各种各样的机制来做同样的代码.
Intent intent = new Intent();  
intent.setType("image/*");  
intent.setAction(Intent.ACTION_GET_CONTENT);  
startActivityForResult(Intent.createChooser(intent,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));
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...