Search code examples
androidandroid-widgetimageviewremoteview

How do I get an image from an sdcard to show up on a widget imageview?


I am trying to make it so that the widget shows an image saved onto the sdcard. so far i have found this:

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);

The commented line causes the widget to crash for some reason. I just want to find a way to make this work.


Solution

  • try as no need to decode image to Bitmap,just get URI of image from image path:

    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);