Java 将单个图像选择从库转换为多个

Java 将单个图像选择从库转换为多个,java,android,Java,Android,我如何更改下面的代码以允许从库中选择多个图像,因为我不确定要选择哪个选项,因为无法使其工作。以下代码适用于单个图像选择: public void openGallery() { Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intentImageContent, load

我如何更改下面的代码以允许从库中选择多个图像,因为我不确定要选择哪个选项,因为无法使其工作。以下代码适用于单个图像选择:

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

试试这个

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

用这个替换你的代码

   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);
}
  @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);
}
然后用这个改变活动结果

   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);
}
  @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);
}
@覆盖
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
试一试{
//拾取图像时
如果(requestCode==1&&resultCode==RESULT\u正常
&&空!=数据){
//从数据中获取图像
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
imagesEncodedList=新的ArrayList();
if(data.getData()!=null){
Uri mimageri=data.getData();
//获取光标
Cursor Cursor=getContentResolver().query(mimageri,
filePathColumn,null,null,null);
//移到第一排
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
imageEncoded=cursor.getString(columnIndex);
cursor.close();
}
}否则{
Toast.makeText(这是“您还没有选择图像”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
Toast.makeText(这个“出错了”,Toast.LENGTH\u LONG)
.show();
}
super.onActivityResult(请求代码、结果代码、数据);
}

这可能对您有用吗