Android 11 多张图片有限制
Android 11 Multiple Images with limit
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType("image/*");
startActivityForResult(intent, Image_REQUEST_CODE);
我用它来 select 多张图片,但没有得到任何限制选项。
ClipData clipData = data.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
String path = getRealPathFromURI(item.getUri());
if (path != null) {
File imageFile = new File(path);
imageList.add(new File(imageFile.toString()));
} else {
Toast.makeText(requireActivity(), "Invalid Path", Toast.LENGTH_SHORT).show();
break;
}
}
}
else {
Uri uri = data.getData();
if (path != null) {
File imageFile = new File(uri);
imageList.add(imageFile);
} else {
Toast.makeText(requireActivity(), "Invalid Path", Toast.LENGTH_SHORT).show();
}
}
//if Images limit is 5
if (imageList.size() > 5) {
Toast.makeText(requireActivity(), "Max 5 photos!", Toast.LENGTH_SHORT).show();
for (int i = imageList.size() - 1; i >= 5; i--) {
imageList.remove(i);
}
}
我使用了这个 onActivityResult() 方法。它会删除多余的照片
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType("image/*");
startActivityForResult(intent, Image_REQUEST_CODE);
我用它来 select 多张图片,但没有得到任何限制选项。
ClipData clipData = data.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
String path = getRealPathFromURI(item.getUri());
if (path != null) {
File imageFile = new File(path);
imageList.add(new File(imageFile.toString()));
} else {
Toast.makeText(requireActivity(), "Invalid Path", Toast.LENGTH_SHORT).show();
break;
}
}
}
else {
Uri uri = data.getData();
if (path != null) {
File imageFile = new File(uri);
imageList.add(imageFile);
} else {
Toast.makeText(requireActivity(), "Invalid Path", Toast.LENGTH_SHORT).show();
}
}
//if Images limit is 5
if (imageList.size() > 5) {
Toast.makeText(requireActivity(), "Max 5 photos!", Toast.LENGTH_SHORT).show();
for (int i = imageList.size() - 1; i >= 5; i--) {
imageList.remove(i);
}
}
我使用了这个 onActivityResult() 方法。它会删除多余的照片