Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Images in ImageView in Android

In my application....there are some images like temp1.jpg, temp2.jpg .....upto temp35.jpg,

so on button clicking, i want to load one-by-one image in ImageView .... i want to do like:

cnt=1;
imagename="temp" + cnt + ".jpg";
cnt++;

so my confusion is that "is there anyway to load an image in imageview from string(imagename variable) like temp1.jpg,etc."

like image 915
Paresh Mayani Avatar asked Jul 07 '10 09:07

Paresh Mayani


People also ask

How do you load an image from a file and set on an ImageView?

If you're working with an Android application, this source code works as a way to load an image from a file: Bitmap bitmap = BitmapFactory. decodeFile(pathToPicture);

What is ImageView in Android?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics. drawable.

How do you handle bitmaps in Android?

For most cases, we recommend that you use the Glide library to fetch, decode, and display bitmaps in your app. Glide abstracts out most of the complexity in handling these and other tasks related to working with bitmaps and other images on Android.


2 Answers

You could try this:

int cnt = 1;
//Bitmap bitmap = BitmapFactory.decodeFile("temp" + cnt + ".jpg");
int imageResource = getResources().getIdentifier("drawable/temp" + cnt + ".jpg", null, getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResource);
imageView.setImageBitmap(bitmap);
cnt++;

Hope that's what you were looking for.

like image 78
xil3 Avatar answered Sep 24 '22 13:09

xil3


Why not something like

File f = new File(PathToFiles + "/temp" + cnt + ".jpg");
if (f.exists()) {
  Drawable d = Drawable.createFromPath(f);
  imageview.setImageDrawable(d);
}
like image 20
slup Avatar answered Sep 24 '22 13:09

slup