将ImageView设置为显示SD卡中的图像?

30

我有一张存储在手机SD卡中的图片,想要在ImageView中显示。我知道文件的位置。在Activity的onCreate方法中,是否有简单的方法可以实现这个目标?

ImageView img = (ImageView) findViewById(R.id.imageView1);    
String path = Environment.getExternalStorageDirectory().toString() "Images/image.jpg";     
img.setsrc = path ;

请告诉我是否有任何方法可以做到这一点。谢谢。

2个回答

88
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageView img;
img.setImageBitmap(bmp);

我在加载一张大小为1.98 MB的位图时遇到了问题,相机拍摄的照片尺寸为16M像素。 - Suhas Bachewar
1
如果Bitmap不断被创建并设置在ImageView上,我们需要调用recycle吗? - Ahmed
当活动结束时,图像会丢失,有没有办法使其永久存在,或者我必须将路径字符串作为文本写入SD卡并每次从中读取? - user3560827

23

我有这个可能会有帮助的代码片段:

File imageFile = new  File("/sdcard/example/image.jpg"); 
if(imageFile.exists()){
    ImageView imageView= (ImageView) findViewById(R.id.imageviewTest);
    imageView.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
}

:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接