gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:05:40 24 4
gpt4 key购买 nike

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

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

最佳答案

用这个替换你的代码

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

并用此更改 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);
}

关于java - 将单个图像选择从图库转换为多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com