多个图像从图库中选择

时间:2017-05-20 05:09:23

标签: android android-gallery

在我的应用程序中,我想从图库中选择多个图像,需要在服务器上上传这些图像。我的代码现在只选择单个图像,下面是我的代码请给我解决方案: -

fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent();
// Show only images, no videos or anything else
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

            }
        });
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));

//                ImageView imageView = (ImageView) findViewById(R.id.imageView);
//                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

意图中有一个额外的“允许多个”选项

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

在您的代码中

Intent intent = new Intent();
// Show only images, no videos or anything else
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// Always show the chooser (if there are multiple options available)
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

然后在你的activityResult

ClipData clipData = data.getClipData();

然后迭代clipData以获取路径

for(int i = 0; i< clipData.getItemCount(); i ++)

{
    Uri uri = clipData.getItemAt(i).getUri();
}

希望这有帮助

答案 1 :(得分:0)

使用此库,使用此功能您可以选择多个图像,也可以直接从相机捕获。这是一个非常简单的集成库。希望这会有所帮助。TedBottomPicker