简体   繁体   English

将图库中的单个图像选择转换为多个

[英]Convert single image selection from gallery to multiple

How can I change the code below to allow multiple images selection from the gallery as I am unsure which option to select as unable to get it working. 我不能更改下面的代码以允许从图库中选择多个图像,因为我不确定选择哪个选项无法使其正常工作。 The code below works for single image selection: 以下代码适用于单张图片选择:

public void openGallery() {
    Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intentImageContent, loadImageResults);
}

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

    if (requestCode == loadImageResults) {
        if (resultCode == RESULT_OK && data != null) {
            Intent intent = new Intent(PhotosActivity.this, PhotosActivity.class);
            intent.putExtra("pickImage", data.getData());
            startActivity(intent);
        }
    }
}

replace your code with this 用这个替换你的代码

   public void openGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
}

And change onActivityResult with this 并以此更改onActivityResult

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        // When an Image is picked
        if (requestCode == 1 && resultCode == RESULT_OK
                    && null != data) {
            // Get the Image from data

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            imagesEncodedList = new ArrayList<String>();
            if(data.getData()!=null){

                Uri mImageUri=data.getData();

                // Get the cursor
                Cursor cursor = getContentResolver().query(mImageUri,
                            filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imageEncoded  = cursor.getString(columnIndex);
                cursor.close();

            }
        } else {
            Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
    }

    super.onActivityResult(requestCode, resultCode, data);
}

Try this 尝试这个

    Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intentImageContent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
    startActivityForResult(intentImageContent, loadImageResults);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM