如何从SD卡中获取图像以显示在窗口小部件imageview上?

时间:2012-07-03 18:46:05

标签: android android-widget imageview remoteview

我正在努力使小部件显示保存在SD卡上的图像。到目前为止我发现了这个:

String imageInSD = String.format("/sdcard/facepalm%d.jpeg", counter%10);    
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
remoteViews.setBitmap(R.id.imageview1, "setImageBitmap", bitmap);
ImageView myImageView = (ImageView)findViewById(R.id.imageview1);
myImageView.setImageBitmap(bitmap);

注释行导致窗口小部件由于某种原因而崩溃。我只想找到一种方法来完成这项工作。

2 个答案:

答案 0 :(得分:0)

尝试不需要将图像解码为Bitmap,只需从图像路径获取图像的URI:

File tempFile = new File(Environment.getExternalStorageDirectory()+"/XX/facepalm%d.jpeg");
Uri  imgUri=Uri.fromFile(tempFile);
ImageView  imageView=(ImageView)findViewById(R.id.imageuri);
imageView.setImageURI(imgUri);

答案 1 :(得分:0)

检查以下代码:

Bitmap bitmap = null;
    Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if(isSDPresent)
    {
       String pathOfImageInSD = Environment.getExternalStorageDirectory()+"/"+"facepalm.png";
       bitmap = BitmapFactory.decodeFile(pathOfImageInSD);
    }
ImageView myImageView = (ImageView)findViewById(R.id.imageview1);
myImageView.setImageBitmap(bitmap);