【问题标题】:When i select image from recent then image is broken in android | Multiple images from recent当我从最近的图像中选择图像时,图像在 android | 中被破坏最近的多张图片
【发布时间】:2021-10-02 06:06:36
【问题描述】:

这个错误主要发生在从最近的文件夹中选择图片时

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

【问题讨论】:

    标签: java android gallery android-glide recent-file-list


    【解决方案1】:

    调用多张图片选择

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

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

    提前致谢 编码愉快

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 2014-08-27
      • 2017-03-27
      • 1970-01-01
      相关资源
      最近更新 更多