0

我想将图像从 sd 卡显示到画廊视图我正在使用代码,它从 drawable 获取图像..但我想更改为 SD 卡

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

但是我想从 SD 卡中获取图像我该如何修改呢?

4

1 回答 1

0

试试这个 ::

这篇博客文章有一个很好的例子。它向您展示了如何使用适配器:http : //mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html 但是我将扩展 CursorAdapter 而不是 BaseAdapter。

要从特定文件夹中获取图像,请参阅链接。

希望这可以帮助你:)

于 2013-01-07T06:09:44.050 回答