从Gallery获取多个图像

时间:2014-12-18 11:12:34

标签: android android-gallery

现在我有了一个imageview。单击图像视图,我可以选择图库/相机意图。选择所需的意图和所需图片我在图像视图中获取图像。这适用于单个图像。

如何获得多张图片。我的意思是imageview []。有没有可用的代码?

2 个答案:

答案 0 :(得分:0)

EXTRA_ALLOW_MULTIPLE

开始意图
Intent intent = new Intent( );
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent,
"select multiple images"), PICK_IMAGE_MULTIPLE);

接收方

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == Activity.RESULT_OK){
     if(requestCode == PICK_IMAGE_MULTIPLE){
            String[] imagesPath = data.getStringExtra("data").split("\\|");
    }
        }
    }

答案 1 :(得分:0)

感谢您的回复,但这是我的代码。现在请让我知道如何在多个imageviews中显示多个图片。如果我需要像gridview这样的占位符,请告诉我吗?

                    ImageView image_view = new ImageView(this);
                image_view.setId(field_id);
                    Uri selectedImage = Uri.parse(field_val);
                    String[] filePath = { MediaStore.Images.Media.DATA };
                    String picturePath;
                    Cursor c = getContentResolver().query(selectedImage,
                            filePath, null, null, null);
                    c.moveToFirst();
                    if(c.moveToFirst() && c.getCount() >= 1)
                    {
                    int columnIndex = c.getColumnIndex(filePath[0]);
                    picturePath = c.getString(columnIndex);
                    c.close();
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmapOptions.inSampleSize = 20;
                    Bitmap thumbnail = (BitmapFactory.decodeFile(
                            picturePath, bitmapOptions));
                    image_view.setImageBitmap(thumbnail);
                    image_view.setTag(selectedImage);
                    }

                    else {
                    image_view.setImageDrawable(getResources().getDrawable(
                            R.drawable.camera_launcher));

                }
                image_view.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        getImage();
                    }
                });

    private void getImage() {

    final CharSequence[] options = { "Take Photo", "Choose from Gallery",
    "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(
            this);
    builder.setTitle("Add Photo!");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (options[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
            } else if (options[item].equals("Choose from Gallery")) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);

            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Bundle extras = data.getExtras();
            try {
                ((ImageView) findViewById(R.id.imageid)).setImageBitmap((Bitmap) extras
                                .get("data"));
                ((ImageView) findViewById(R.id.imageid))).setTag(R.string.imgtag,
                                data.getDataString());

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            String picturePath;
            Cursor c = getContentResolver().query(selectedImage, filePath,
                    null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            picturePath = c.getString(columnIndex);
            c.close();
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inSampleSize = 20;
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath,
                    bitmapOptions));
            ((ImageView) findViewById(R.id.imageid))).setImageBitmap(thumbnail);
            ((ImageView) findViewById(R.id.imageid))).setTag(R.string.imgtag,
                            selectedImage.toString());

        }
    }
}