我想从手机中选择多张图片,但我只获得1张图片,现在我正在使用意图
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);
但它仍然只选择一个图像。所以如何设置图像选择的限制超过1以及如何在onActivityResult
中处理它。
答案 0 :(得分:0)
首先, 您需要对多个图像执行哪些操作,这很重要
其次,你可以使用数组列表作为相同的位图数组列表,并进一步传递该列表,你可以从该列表中访问所有图像..请尝试并恢复..
答案 1 :(得分:0)
尝试以下代码:
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) {
if(requestCode ==1)
if (data != null) {
ClipData clipData = null;
try {
clipData = data.getClipData();
} catch (Exception exception) {
e.printStackTrace();
}
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount();i++) {
// Get Uri for the image
Uri selectedImage = data.getClipData().getItemAt(i).getUri();
try {
String imagePath = getRealPathFromURI(selectedImage, this); /* Do whatever you want to do with imagePath */
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
private String getRealPathFromURI(Uri contentURI, Context context) {
String filePath = "";
Pattern p = Pattern.compile("(\\d+)$");
Matcher m = p.matcher(contentURI.toString());
if (!m.find()) {
Log.e("", "ID for requested image not found: " + contentURI.toString());
return filePath;
}
String imgId = m.group();
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{imgId}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}