java - 当我从最近的图像中选择图像时,图像在 android | 中被破坏 最近的多张图片
问题描述
从最近的文件夹中选择图像时,通常会发生此错误
class com.bumptech.glide.load.engine.GlideException: Received null model
解决方案
调用多张图片选择
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
*gallery*.launch(i);
画廊基本上 startActivityForResult(i,123) 和 OnActivityResult 方法已被弃用,画廊是替代品,定义如下
ActivityResultLauncher<Intent> gallery = choosePhotoFromGallery();
并且choosePhotoFromGallery()是下面定义的方法
private ActivityResultLauncher<Intent> choosePhotoFromGallery() {
return registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
try {
if (result.getResultCode() == RESULT_OK) {
if (null != result.getData()) {
if (result.getData().getClipData() != null) {
ClipData mClipData = result.getData().getClipData();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
String imageFilePathColumn = getPathFromURI(this, uri);
productImagesList.add(imageFilePathColumn);
}
} else {
if (result.getData().getData() != null) {
Uri mImageUri = result.getData().getData();
String imageFilePathColumn = getPathFromURI(this, mImageUri);
productImagesList.add(imageFilePathColumn);
}
}
} else {
showToast(this, "You haven't picked Image");
productImagesList.clear();
}
} else {
productImagesList.clear();
}
} catch (Exception e) {
e.printStackTrace();
showToast(this, "Something went wrong");
productImagesList.clear();
}
});
}
getPathFromURI()是下面定义的方法
public String getPathFromURI(Context context, Uri contentUri) {
OutputStream out;
File file = getPath();
try {
if (file.createNewFile()) {
InputStream iStream = context != null ? context.getContentResolver().openInputStream(contentUri) : context.getContentResolver().openInputStream(contentUri);
byte[] inputData = getBytes(iStream);
out = new FileOutputStream(file);
out.write(inputData);
out.close();
return file.getAbsolutePath();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
getPath( )是
private File getPath() {
File folder = new File(Environment.getExternalStorageDirectory(), "Download");
if (!folder.exists()) {
folder.mkdir();
}
return new File(folder.getPath(), System.currentTimeMillis() + ".jpg");
}
提前谢谢你 快乐编码
推荐阅读
- ruby-on-rails - 通过 Ruby 将 Active Admin 中的 jsoneditor 设为只读?
- reactjs - 带有 react-apexcharts 的点击事件
- regex - 正则表达式匹配我需要的文本之间的文本,最大字符数为 20
- django - 如何使用 Django 生成 Word 文档
- single-page-application - 有一种方法可以避免在 PWA 中的每个请求都下载 Spa Bundle
- python - 在 Python 中跨多个模块的每个对象日志记录
- azure-devops - 是否可以在 YAML 文件中定义分支策略?
- python - 如何在函数外使用函数中的变量
- graphviz - 如何在 Graphviz 中垂直对齐子图簇?
- r - 根据另一列的值平均一列的特定行