0

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.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • 1
    Hardcoding your image's location to `/sdcard` is not a good idea. In a lot of new devices, the sdcard is mounted under `/emmc` – Anirudh Ramanathan Jul 03 '12 at 18:50
  • it may be helpful to add that i am completely new to android so... i dont even know what im doing... – Mathew Vejar Miravite Jul 03 '12 at 18:51
  • to the OP, I suggest you read up the basics and fundamentals of this before embarking on this otherwise you'd just get frustrated and give up. There's plenty of resources out there, in fact, I recommend Commonsware series, to give you an overview of Android, fundamentals of Java etc :) – t0mm13b Jul 03 '12 at 18:59
  • but i want to know why you are decoding image to Bitmap because it's possible to set image directly in ImageView from SD-CARD without decoding it to Bitmap using Image URI? – ρяσѕρєя K Jul 03 '12 at 19:02
  • http://commonsware.com/ that's where I learnt mine from among other resources :) really I should not be advertising that per se... but hey, its my recommendation and word of mouth... the SO user commonsware would be quite pleased :) :P – t0mm13b Jul 03 '12 at 19:04
  • @imran khan how do i do that? – Mathew Vejar Miravite Jul 03 '12 at 19:07
  • @MathewVejarMiravite : see my answer or if you are decoding Image to Bitmap then be Careful this maybe cause memory Leaks for your Application. Declare Bitmap as WeakReference because using it – ρяσѕρєя K Jul 03 '12 at 19:11

2 Answers2

0

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);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • this solved it and i thank you though i didnt exactly use all of it i only used File tempFile = new File(Environment.getExternalStorageDirectory()+"/XX/facepalm%d.jpeg"); Uri imgUri=Uri.fromFile(tempFile); – Mathew Vejar Miravite Jul 03 '12 at 19:35
0

Check the following code:

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);
Arun George
  • 18,352
  • 4
  • 28
  • 28