如何从SD卡获取图像并在gridview中显示它

时间:2012-08-28 21:53:02

标签: android gridview android-imageview android-sdcard

实际上我尝试使用这种编码它工作正常。即,在模拟器中完美地显示所有图像但是移动它会反复显示相同的图像。我也附加了适配器类

File file[] = Environment.getExternalStorageDirectory().listFiles();
            String strFile = "";
            for (int i = 0; i < file.length; i++) {
                strFile += file[i].getAbsolutePath() + " :: ";
                System.out.println("\n");
            }
            Log.i("test", strFile);
            String[] img = { MediaStore.Images.Thumbnails._ID };
            imagecursor = managedQuery(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
                    null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
            image_column_index = imagecursor
                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
            count = imagecursor.getCount();
            imagegrid = (ListView) findViewById(R.id.PhoneImageGrid);
            imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
            imagegrid.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position,
                        long id) {
                    System.gc();
                    String[] proj = { MediaStore.Images.Media.DATA };
                    actualimagecursor = managedQuery(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
                            null, null, null);
                    actual_image_column_index = actualimagecursor
                            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    actualimagecursor.moveToPosition(position);
                    String i = actualimagecursor
                            .getString(actual_image_column_index);
                    System.gc();
                    Intent intent = new Intent(getApplicationContext(),
                            ViewImage.class);
                    intent.putExtra("filename", i);
                    startActivity(intent);
                }
            });
        }

适配器类:

public class ImageAdapter extends BaseAdapter {
            private Context mContext;

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

            public int getCount() {
                return count;
            }

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

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

            public View getView(int position, View convertView, ViewGroup parent) {
                System.gc();
                ImageView i = new ImageView(mContext.getApplicationContext());
                if (convertView == null) {
                    imagecursor.moveToPosition(position);
                    int id = imagecursor.getInt(image_column_index);
                    i.setImageURI(Uri.withAppendedPath(
                            MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
                                    + id));
                    i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    i.setLayoutParams(new GridView.LayoutParams(92, 92));
                } else {
                    i = (ImageView) convertView;
                }
                return i;

0 个答案:

没有答案