从SD卡导入图像并在图库视图中显示

时间:2012-02-22 13:33:40

标签: android android-sdcard

我将图片从我的SD卡导入图库.. 导入的所有图像都显示在库中。 当我点击图库中的图像时,我将此图像显示为剩余布局中的图像视图。但那时画廊的一些图像消失了。 我不知道为什么会这样。

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        projection = new String[] { MediaStore.Images.Media._ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN,
                MediaStore.Images.Media.DISPLAY_NAME };

        imagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        cursor = managedQuery(imagesUri, projection, "", null,
                MediaStore.Images.Media._ID);

        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        sdcardImages = (Gallery) findViewById(R.id.gallery);
        sdcardImages.setAdapter(new ImageAdapter(this));

        sdcardImages.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView parent, View v, int position,
                    long id) {
                String[] projection = { MediaStore.Images.Media.DATA };

                cursor = managedQuery(imagesUri, projection, null, null,
                        MediaStore.Images.Media._ID);
                columnIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                cursor.moveToPosition(position);
                imagePath = cursor.getString(columnIndex);
                Log.i("Image Path ", imagePath);
                imgFile = new File(imagePath);
                if (imgFile.exists()) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                            .getAbsolutePath());
                    imageSelected = (ImageView) findViewById(R.id.selectedImage);
                    imageSelected.setImageBitmap(myBitmap);
                    imageSelected.setOnClickListener(GetImages.this);

                }

            }
        });

    }

    private class ImageAdapter extends BaseAdapter {
         int mGalleryItemBackground;
        private Context context;

        public ImageAdapter(Context localContext) {
            context = localContext;
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return cursor.getCount();
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);
                cursor.moveToPosition(position);
                int imageID = cursor.getInt(columnIndex);
                picturesView.setImageURI(Uri.withAppendedPath(imagesUri, ""
                        + imageID));
                picturesView
                        .setLayoutParams(new Gallery.LayoutParams(100, 100));
                picturesView.setScaleType(ImageView.ScaleType.FIT_XY);

                picturesView.setBackgroundResource(mGalleryItemBackground);
            } else {
                picturesView = (ImageView) convertView;
            }
            return picturesView;
        }
    }

    @Override
    public void onClick(View arg0) {

        final Dialog dialog = new Dialog(GetImages.this);
        LayoutInflater inflater = (LayoutInflater) GetImages.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.editor_layout, null);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);

        Button button = (Button) view.findViewById(R.id.delete_button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                boolean deleted = imgFile.delete();
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
                        .parse("file://"
                                + Environment.getExternalStorageDirectory())));
                if (deleted) {
                    dialog.dismiss();
                    Toast.makeText(GetImages.this, "Deleted",
                            Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
        Button button1 = (Button) view.findViewById(R.id.edit_button);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(GetImages.this, EditActivity.class);
                intent.putExtra("imagePath", imgFile);
                intent.putExtra("Path", imagePath);
                startActivity(intent);
            }
        });
        dialog.setContentView(view);
        dialog.show();

    }

0 个答案:

没有答案