0

i am working on a image GridView app in which i am adding images in array from drawable, but now i want to add images in array from sd card.

note: i want to add images from specific folder like: "sdcard/android/data/com.example.images"

GridView Class

package com.example.androidhive;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class AndroidGridLayoutActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(),
                        FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

ImageAdapter Class:

package com.example.androidhive;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;



    // Keep all Images in array
    public Integer[] mThumbIds = {
            // R.string.http_icons_iconarchive_com_icons_martz90_circle_512_android_icon_png
            R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3,
            R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_6,
            R.drawable.pic_7, R.drawable.pic_8, R.drawable.pic_9,
            R.drawable.pic_10, R.drawable.pic_11, R.drawable.pic_12,
            R.drawable.pic_13, R.drawable.pic_14, R.drawable.pic_15 };

    // Constructor
    public ImageAdapter(Context c) {
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;
    }

}

example of what i want:

// Keep all Images in array
    public Integer[] mThumbIds = {          
// how to add images from sd card here
 };

how do i add images to array from sd card instead of drawable.

user3739970
  • 591
  • 2
  • 13
  • 28

2 Answers2

0

Just put all filenames from that specific folder in a ArrayList<String>. Then change getView() a bit to let the ImageView load from file.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • can you help me with my code or give me sample because i am not understanding – user3739970 Aug 08 '14 at 09:49
  • Yes i can help you. Please show your code. Explain what you do not understand to begin with. – greenapps Aug 08 '14 at 09:50
  • i updated my code please see and help me to add images from sd card to array `public Integer[] mThumbIds = { //add images from sd card }` – user3739970 Aug 08 '14 at 09:57
  • That code was already there before i posted my answer. So you are repeating yourself only. That code is wrong as you cannot represent image filenames by integer values like for drawable resources. So take a String ArrayList as I proposed. – greenapps Aug 08 '14 at 10:00
  • hey i found this : http://karanbalkar.com/2014/05/displaying-images-from-sd-card-in-android/ now will u help me to adjust this code with my code. – user3739970 Aug 08 '14 at 11:00
  • Throw away your code and take all the code from that site and you are done. – greenapps Aug 08 '14 at 11:02
0

You can create a Drawable from a Bitmap, you should check out a library like "Universal Image Loader" and then use

Drawable d = new BitmapDrawable(getResources(),bitmap);

FrankMonza
  • 2,024
  • 16
  • 26