当我从最近选择图像时,图像在 android 中损坏 |最近的多张图片 [英] When i select image from recent then image is broken in android | Multiple images from recent

查看:31
本文介绍了当我从最近选择图像时,图像在 android 中损坏 |最近的多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

此错误主要发生在从最近的文件夹中选择图像时

class com.bumptech.glide.load.engine.GlideException: 收到空模型

解决方案

调用多张图片选择

示例预览

 Intent i = new Intent(Intent.ACTION_GET_CONTENT);i.addCategory(Intent.CATEGORY_OPENABLE);i.setType("image/*");i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);*画廊*.launch(i);

<块引用>

gallery 基本上不推荐使用 startActivityForResult(i,123) 和 OnActivityResult 方法,gallery 是替代方法,定义如下

ActivityResultLauncher画廊 = selectPhotoFromGallery();

choosePhotoFromGallery()是下面定义的方法

private ActivityResultLauncher选择照片从画廊(){返回 registerForActivityResult(新的 ActivityResultContracts.StartActivityForResult(),结果 ->{尝试 {如果(result.getResultCode()== RESULT_OK){如果(空!= result.getData()){如果 (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);}} 别的 {如果 (result.getData().getData() != null) {Uri mImageUri = result.getData().getData();String imageFilePathColumn = getPathFromURI(this, mImageUri);productImagesList.add(imageFilePathColumn);}}} 别的 {showToast(this, "你还没有选择图片");productImagesList.clear();}} 别的 {productImagesList.clear();}} 捕获(异常 e){e.printStackTrace();showToast(this, 出了点问题");productImagesList.clear();}});}

getPathFromURI()是下面定义的方法

 public String getPathFromURI(Context context, Uri contentUri) {输出流输出;文件文件 = getPath();尝试 {如果 (file.createNewFile()) {InputStream iStream = 上下文 != null ?context.getContentResolver().openInputStream(contentUri) : context.getContentResolver().openInputStream(contentUri);byte[] inputData = getBytes(iStream);out = new FileOutputStream(file);out.write(inputData);关闭();返回 file.getAbsolutePath();}} catch (IOException e) {e.printStackTrace();}返回空;}private byte[] getBytes(InputStream inputStream) 抛出 IOException {ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();int bufferSize = 1024;字节[]缓冲区=新字节[缓冲区大小];int len = 0;while ((len = inputStream.read(buffer)) != -1) {byteBuffer.write(buffer, 0, len);}返回 byteBuffer.toByteArray();}

getPath()

私有文件 getPath() {文件夹 = new File(Environment.getExternalStorageDirectory(), 下载");如果 (!folder.exists()) {文件夹.mkdir();}返回新文件(文件夹.getPath(),System.currentTimeMillis()+.jpg");}

提前致谢 编码愉快

This error occurs mostly times when select images from recent folder

class com.bumptech.glide.load.engine.GlideException: Received null model

解决方案

Call multiple images select

Sample Preview

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

gallery basically startActivityForResult(i,123) and OnActivityResult method is deprecated, the gallery is alternative which is defined below

ActivityResultLauncher<Intent> gallery = choosePhotoFromGallery();

and choosePhotoFromGallery() is method which is define below

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

and getPathFromURI() is method which define below

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

and the getPath() is

private File getPath() {
    File folder = new File(Environment.getExternalStorageDirectory(), "Download");
    if (!folder.exists()) {
        folder.mkdir();
    }
    return new File(folder.getPath(), System.currentTimeMillis() + ".jpg");
}

THANK YOU IN ADVANCE HAPPY CODING

这篇关于当我从最近选择图像时,图像在 android 中损坏 |最近的多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆