0

I would simply like to add photos from a predetermined file to a GridView and I can't figure out how to do this. I have an Adapter that handles the GridView that I learned from this example and I haven't been able to figure out how to set the mThumbIds array to images from a file. I think I have to use something like: ImageView.setImageUri(Uri.fromFile(new File("myPath"))); instead of setImageResource but I could be wrong. This is my first time doing something like this and any help would really be appreciated!

EDIT:

Current Code: (with this I'm getting a NullPointer on: for(File f : dir.listFiles()){ which in the doc it says it returns null if it is not a directory. The code in new File(...) is the same I use to create the directory so I know the path is correct, when the application starts it creates the directory with no pictures in the folder until the user takes one. I thought then that this was the issue however it still closes when there is a photo in the directory.)

public static class HomeFragment extends Fragment{

    public HomeFragment(){

    }
    View rootView;
    GridView gridView;
    List<Drawable> list;
    File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyAppFolder");
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup contatiner,
            Bundle savedInstanceState){

        for(File f : dir.listFiles()){
            Bitmap original = BitmapFactory.decodeFile(f.getAbsolutePath());
            Drawable drawable = new BitmapDrawable(getResources(), original);
            list.add(drawable);
        }
        rootView = inflater.inflate(R.layout.home_layout,
                contatiner, false);
        gridView = (GridView) rootView.findViewById(R.id.homeGridView);
        gridView.setAdapter(new HomeAdapter(getActivity(), list));


        return rootView;
    }

}


public class HomeAdapter extends BaseAdapter {
private Context mContext;
private List<Drawable> mPictures;

public HomeAdapter(Context c, List<Drawable> list) {
    mContext = c;
    mPictures = list;
}

public int getCount() {
    return mPictures.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}


public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) { 
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageDrawable(mPictures.get(position));
    return imageView;
}
}
Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79
  • Your code looks fine to me. Try this (just to see if it works): replace `File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyAppFolder");` with `File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);`. I am assuming DCIM folder has some image files. – Vikram Aug 15 '13 at 16:57
  • Ok, I did that and now I'm getting a `NullPointerException` on: `list.add(drawable);` – Ryan Sayles Aug 15 '13 at 17:08
  • Let's talk here: [Link](http://chat.stackoverflow.com/rooms/35533/discussion-between-vikram-and-rsayles3) – Vikram Aug 15 '13 at 17:09
  • By the way, do you want me to update my answer? Even as it is now, it sends you in the right direction. – Vikram Aug 15 '13 at 20:33

1 Answers1

2

You can get the Bitmap using BitmapFactory.decodeFile(filePath):

Bitmap original = BitmapFactory.decodeFile(filePath);

// Scale the Bitmap if needed       
Bitmap scaled = Bitmap.createScaledBitmap(original, newWidth, newHeight, true);

To create a Drawable from this Bitmap:

// Use mContext.getResources if you are doing this in the Adapter
Drawable drawable = new BitmapDrawable(getResources(), scaled);

Change the type of mThumbIds:

private Drawable[] mThumbIds = { drawable1, drawable2, drawable3....... };

Use ImageView.setImageDrawable(Drawable):

imageView.setImageDrawable(mThumbIds[position]);

A better approach would be to create mThumbIds array in your Activity and pass it as an argument when you initalize ImageAdapter. You'll need to change ImageAdapter's constructor.

Edit 1:

You can use a List<Drawable> to overcome the unknown size issues. I am going to assume that you are able to create Drawable objects and initialize them using the information above:

Drawable d1 = ....
Drawable d2 = ....
Drawable d3 = ....
....
....

List<Drawable> list = new ArrayList<Drawable>();
list.add(d1);
list.add(d2);
....

gridview.setAdapter(new ImageAdapter(this, list));

Change your ImageAdapter's code to:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<Drawable> mThumbIds;

    public ImageAdapter(Context c, List<Drawable> list) {
        mContext = c;
        mThumbIds = list;
    }

    public int getCount() {
        return mThumbIds.size();
    }

    ....
    ....

In ImageAdapter's getView():

imageView.setImageDrawable(mThumbIds.get(position));

Remove:

private Integer[] mThumbIds = { ..... };
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thank you! I just had one more question, what do I pass into the array? Since I don't know what the size will be I can explicitly say drawable1, drawable2, ect. Do I create a loop to place them into the array? – Ryan Sayles Aug 14 '13 at 21:25
  • @rsayles3 See **Edit1** above. – Vikram Aug 14 '13 at 21:50
  • I still don't know how to initialize the `Drawable` objects, I want my program to be able to take a photo then immediately add it to the `GridView`, so the size of the `ArrayList` is never going to be the same. – Ryan Sayles Aug 14 '13 at 22:23
  • @rsayles3 What do you mean by `take a photo`? Is this photo saved on the SDCard? Or, will you be using the camera application. – Vikram Aug 14 '13 at 22:28
  • I'm calling the camera application: `android.provider.MediaStore.ACTION_IMAGE_CAPTURE` and saving the photo that is taken to a custom made folder in the local DCIM folder. So I want to take the photos from that custom made folder and display them in a `GridView` – Ryan Sayles Aug 14 '13 at 22:56
  • @rsayles3 I am sorry, but I'm not sure what more info I can provide you with. My original answer precisely covered how you can retrieve images from a folder on sdcard. My edit showed that by using an ArrayList, you won't have to specify a `size`. The list can have as many elements as required. – Vikram Aug 14 '13 at 23:07
  • I think I understand your code, but it seems to me that it would only work if you know what is in the file already. Otherwise how do I initiate or even create `Drawable d1` `Drawable d2`...ect. I would need something like pseudo-code: `for(int i = 0; i <= numberofitems; i++ ){list.add(i)}` – Ryan Sayles Aug 14 '13 at 23:44
  • @rsayles3 If you know the path to the folder where you save the images, create a File: `File dir = new File(folderPath);`. Then, use a for loop: `for(File f : dir.listFiles()) { Bitmap original = BitmapFactory.decodeFile(f.getAbsolutePath()); Drawable drawable = new BitmapDrawable(getResources(), original); list.add(drawable); }`. – Vikram Aug 15 '13 at 06:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35533/discussion-between-vikram-and-rsayles3) – Vikram Aug 15 '13 at 16:58