Ryan Sayles
Ryan Sayles

Reputation: 3431

Set images from file into GridView

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;
}
}

Upvotes: 0

Views: 2141

Answers (1)

Vikram
Vikram

Reputation: 51571

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 = { ..... };

Upvotes: 2

Related Questions