user826323
user826323

Reputation: 2338

android - create gallery with photos from sdcard

I am trying to create gallery with photos from sdcard. I have a database table. One of the columns in the table has a path to a photo.

I am following this link as a guideline, but this sample code retrieves image ids from res/drawable directory. I am not sure how to modify it to make my code work.

http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html

But as mentioned, I want to display photos with whatever images exist in the database table. The following code is ImageAdapter.

    public class ImageAdapter extends BaseAdapter {

    private Context ctx;

    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.milestone_style);
        imageBackground = ta.getResourceId(R.styleable.milestone_style_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {
        return imagePhotosLocations.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        if (milestones != null && !milestones.isEmpty()) {
            Milestone milestone = milestones.get(0);
            String imageFileLocation = milestone.getFileLocation();
            System.out.println("imageFileLocation="+imageFileLocation);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 26;
            Bitmap b;
            ImageView iv = new ImageView(ctx);
            try {
                if (imageFileLocation.startsWith("content://")) {
                    b = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(imageFileLocation)), null, options);
                } else {
                    b = BitmapFactory.decodeFile(imageFileLocation, options);
                }
                iv.setImageBitmap(b);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
                // milestoneImageView.setBackgroundResource(imageBackground);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            return iv;
        }
        return null;
    }
}

And this is one of the methods in activity.

private String[] imagePhotosLocations;

private void init() {
    milestones = getMilestones();
    imagePhotosLocations = new String[milestones.size()];
    int index = 0;
    for (Milestone milestone : milestones) {
        imagePhotosLocations[index++] = milestone.getFileLocation();
    }
}

private void initializeGallery() {
    milestoneImageView = (ImageView) this.findViewById(R.id.imagePhoto);
    Gallery milestoneGallery = (Gallery) this.findViewById(R.id.milestoneGallery);
    if (milestoneGallery == null) {
        throw new IllegalArgumentException("milestoneGallery can not be null.");
    }
    milestoneGallery.setAdapter(new ImageAdapter(this));
    milestoneGallery.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int which, long arg3) {
            String imageFileLocation = imagePhotosLocations[which];
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 26;
            Bitmap b;
            try {
                if (imageFileLocation.startsWith("content://")) {
                    b = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(imageFileLocation)), null, options);
                } else {
                    b = BitmapFactory.decodeFile(imageFileLocation, options);
                }
                milestoneImageView.setImageBitmap(b);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    });
}

Upvotes: 0

Views: 722

Answers (1)

user1157934
user1157934

Reputation: 51

Here are the steps which I used,

  1. First check if the SD card is mounted.
  2. Query your database.Details on this can be found here:
    SQLite tutorial.
  3. The Query returns a cursor for the result set.
  4. Use a custom Cursor Adapter (Refer: Custom Cursor Adapter)
  5. Some times if the image is too large ,loading the image into bitmap may exceed VM budget. So refer this article Avoiding out of memory issue while loading image

  6. So the cursor adapter returns an image view as its done in your code.

  7. finally bind this custom adapter to your gallary.

hope this answers your question.

Upvotes: 1

Related Questions