Импортирование изображений с SDCard и показ в виде галереи - PullRequest
3 голосов
/ 22 февраля 2012

Я импортирую изображения из моей SDCard в галерею .. Все импортированные изображения отображаются в галерее. Когда я нажимаю на изображение в галерее, я показываю это изображение в виде изображения в оставшемся макете. Но в это время некоторые изображения из галереи исчезают. Я не знаю, почему это происходит.

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();

    }
...