Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从SD卡导入图像并在库视图中显示_Android_Android Sdcard - Fatal编程技术网

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

Android 从SD卡导入图像并在库视图中显示,android,android-sdcard,Android,Android Sdcard,我正在将SD卡中的图像导入gallery。。 所有导入的图像都显示在库中。 当我点击gallery中的图像时,我将该图像显示为剩余布局中的图像视图。但在那个时候,画廊里的一些图像消失了。 我不知道为什么会这样 protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState);

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

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

    }

你能分享你现在使用的代码吗?