0

I have some images that are saved to a directory on the Android device when the application starts--I would like to be able to display these images in a Gallery, but so far I haven't been able to do it.

I was following the sample Gallery code here, but it uses drawable resource IDs instead of file paths. I found this solution that is similar to what I'm looking for, except it uses ImageView instead of Gallery.

So the code using ImageView would look something like this:

File imgFile = new  File(“/data/data/com.myproject.example/files/someImage.png”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}

The above code works, but I'm not sure how to do it with a Gallery. I have been searching for answers and trying different things, but I'm v new to Android development and I feel like I'm in a bit over my head.

Any help is appreciated. Thank you in advance.

Community
  • 1
  • 1
mbil
  • 106
  • 6

2 Answers2

0

Basically I think you just need to put your two examples together.

Use the HelloGallery example as a start, however you want to change the code inside the getView() method of the ImageAdapter to call setImageBitmap() on the ImageView instead of setImageResource().

You will need an array/collection of file paths of images you want to load.

denizmveli
  • 2,558
  • 1
  • 23
  • 18
0

What you need to do is something like this:

public ImageAdapter(Context c, int itemId) {
    context = c;

    imgArr = GlobalStore.getItem(itemId).getPhotos();
    TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
    mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
    attr.recycle();
}

as you can see this is basically a copy from Gallery tutorial. In the constructor imgArr variable is loaded with an array of JPG file names. These were for example read from a database.

Then in the getView function you have something like this...

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(context);


    String tmpStr = appContext.getFilesDir() + File.separator + "photos" + File.separator + imgArr.get(position);
    Bitmap bitmap = BitmapFactory.decodeFile(tmpStr);
    imageView.setImageBitmap(bitmap);
    imageView.setLayoutParams(new Gallery.LayoutParams(350, 300));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setBackgroundResource(mGalleryItemBackground);

    return imageView;
}

As you can see getFilesDir() gets your applications data location where it stores files, then let's imagine all photos are in "photos" directory, you build a path and attach a file name from the imgArr array. Since this is called for every photo you just use the passed position variable.

If you don't have an array of photos then maybe the way is to build it by reading the directory where you store photos, load all of the filenames in an array and then do this.

Then you do the rest on the gallery side as in the gallery tutorial.

Dusko
  • 628
  • 2
  • 6
  • 24