简体   繁体   English

当我从最近选择图像时,图像在 android 中损坏 | 最近的多张图片

[英]When i select image from recent then image is broken in android | Multiple images from recent

从最近的文件夹中选择图像时,通常会发生此错误

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画廊基本上 startActivityForResult(i,123) 和 OnActivityResult 方法已弃用,画廊是替代品,定义如下

ActivityResultLauncher<Intent> gallery = choosePhotoFromGallery();

and choosePhotoFromGallery() is method which is define below并选择PhotoFromGallery()是下面定义的方法

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

and the getPath() isgetPath()

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谢谢你提前快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Android中的最新应用中刷出应用后,服务停止 - Service stopped when app is swiped out from recent apps in Android 当应用程序从android中的最新应用程序列表中删除时,服务停止 - Service stopped when the app removes from recent apps list in android Android - 从最近的应用程序中滑动应用程序时停止服务 - Android - Service stops when swiping app from recent apps Android:从最近使用的应用程序按钮关闭应用程序时未调用OnDestroy - Android: OnDestroy isn't called when I close the app from the recent apps button 我如何从 android 工作室的图库中获取 select 多个图像 - how I can select multiple images from gallery in android studio 如何从多个较小的图像构建图像 - How do I build an Image from multiple smaller Images 从“最近使用的应用程序”列表中删除应用程序后,Android服务将重新启动 - Android service gets restarted when application is removed from Recent Apps list 在android生命周期中,当从最近的应用列表中滑动应用时,是否会调用任何独特的方法? - In android lifecycle, is there any unique method gets called when app is swiped from recent app list? 选择一个图像而不是多个图像时出现android错误 - android error when select one image not multiple 第三次从图库中选择图像时内存不足 - Out of memory when select image from gallery for the third times Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM