0

I want to display an image from sd card to gallery view I am using the code,In which it is getting the Image from drawable..But I want to change to to SD card

private Gallery gallery;
private ImageView imgView;
int position;
private Integer[] Imgid = { R.drawable.a_1, R.drawable.a_2, R.drawable.a_3,
        R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    position = 0;
    imgView = (ImageView) findViewById(R.id.ImageView01);
    imgView.setImageResource(Imgid[0]);

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            imgView.setImageResource(Imgid[position]);
            GalleryExample.this.position = position;
        }
    });


    imgView.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {

            AlertDialog alertDialog = new AlertDialog.Builder(
                    GalleryExample.this).create();
            alertDialog.setTitle("Confirmation");
            alertDialog
                    .setMessage("Do you want to set this image as wallaper?");
            alertDialog.setButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                            Bitmap bitmap = BitmapFactory.decodeResource(
                                    getResources(), Imgid[position]);
                            try {
                                GalleryExample.this.setWallpaper(bitmap);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            Log.d("Gallery Example", "Image setted.");

                        }
                    });

            alertDialog.show();
            return true;
        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(
                R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

    public int getCount() {
        return Imgid.length;
    }

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

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

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

        imgView.setImageResource(Imgid[position]);
        imgView.setLayoutParams(new Gallery.LayoutParams(100, 100));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

But iwant to get the image from Sd card How can i modify this??

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
apcdgworks
  • 263
  • 1
  • 4
  • 14
  • Retrieve the path of all image and put it into array. then set into imageview. – Ajay S Jan 07 '13 at 06:12
  • It's already a dublicate question. http://stackoverflow.com/questions/2901342/how-to-display-images-from-sd-card-in-a-galleryview. – Shadow Jan 07 '13 at 06:21

1 Answers1

0

Try this ::

This blog post has a good example on that. It shows you how to do the adapter: http://mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html However I would be extending a CursorAdapter instead of a BaseAdapter.

To get the image from the specific folder refer this link.

Hope this helps you :)

Community
  • 1
  • 1
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
  • In this link it displaying all the Images in sd card I want only from a specific folder,How can i do it?? – apcdgworks Jan 07 '13 at 06:26
  • There is one link which fetch the image from specific folder of the sd card.check that link.It will help you to solve your problem. :) – AndroidLearner Jan 07 '13 at 06:35
  • not at all,It will be fetching all the imaged from the SD card and displaying as as grid view,I want it as GalleryView From the specific folder.. – apcdgworks Jan 07 '13 at 06:38
  • which link you are refering first or second one ?I have set two links in my answer – AndroidLearner Jan 07 '13 at 06:39
  • I have already checked both the links.Refer the second one link and see the answer.It is what you want . – AndroidLearner Jan 07 '13 at 06:50
  • i got it but in the second link it showing an error that cannot instantiate the type Image adapter in the line................adapter = new ImageAdapter(this, bmpArray, fileName); gridView = (GridView)findViewById(R.id.grid_view); gridView.setAdapter(adapter); – apcdgworks Jan 07 '13 at 06:56
  • Instead of "this",Pass the "YourActivityName.this". – AndroidLearner Jan 07 '13 at 07:00
  • Try googling for that.I have found this ::http://dikshantrawal.blogspot.in/2012/03/complete-code-display-image-from.html Best of luck :) – AndroidLearner Jan 07 '13 at 07:12
  • If my links helpful to you,don't forget to accept and upvote it as answer :) – AndroidLearner Jan 07 '13 at 07:27