【问题标题】:How to solve error inserting image while using Glide?如何解决使用 Glide 插入图像时出错的问题?
【发布时间】:2015-07-28 20:11:00
【问题描述】:

我正在尝试使用 Glide 加载图片,然后用它创建一个意图来裁剪它。

它适用于 API 21+ 设备,但不在以下设备上。

这是代码:

Glide.with(context)
    .load(wallurl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            if (resource != null) {
                if (dialogApply != null) {
                    dialogApply.dismiss();
                }
                dialogApply = new MaterialDialog.Builder(context)
                        .content(R.string.downloading_wallpaper)
                        .progress(true, 0)
                        .cancelable(false)
                        .show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Uri wallUri = getImageUri(context, resource);
                            if (wallUri != null) {
                                dialogApply.dismiss();
                                Intent setWall = new Intent(Intent.ACTION_ATTACH_DATA);
                                setWall.setDataAndType(wallUri, "image/*");
                                setWall.putExtra("png", "image/*");
                                startActivityForResult(Intent.createChooser(setWall, getString(R.string.set_as)), 1);
                            } else {
                                dialogApply.setContent(R.string.error);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    });

public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

这是我得到的错误:

E/MediaStore﹕ Failed to insert image
    java.io.FileNotFoundException: No such file or directory
            at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
            at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:611)
            at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
            at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
            at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
            at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:902)
            at jahirfiquitiva.iconshowcase.activities.ViewerActivity.getImageUri(ViewerActivity.java:366)
            at jahirfiquitiva.iconshowcase.activities.ViewerActivity$5$2$1.run(ViewerActivity.java:335)
            at java.lang.Thread.run(Thread.java:841)

有人可以帮我解决它吗?提前致谢。

【问题讨论】:

    标签: android image android-contentresolver mediastore android-glide


    【解决方案1】:

    您可以尝试像这样预取图像

                FutureTarget<File> future = Glide.with(applicationContext)
                        .load(yourUrl) 
                        .listener(new RequestListener<String, GlideDrawable>() {
                            @Override
                            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                                return true;
                            }
    
                            @Override
                            public boolean onResourceReady(Bitmap resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    
                            //do your stuff
    
                            return false;
                    }
                })
                .into(500, 500);
    

    在其他活动中,您只需正常加载图像,因为它将被缓存

    【讨论】:

    • 它说 RequestListener() 不能应用于 RequestListener() ... 我该怎么办?
    • 我刚刚编辑了我的答案。您需要使用 RequestListener 而不是 RequestListener
    • 它给了我一个 GlideDrawable,我需要一个位图......或者我将如何获得 GlideDrawable uri?
    • 哦,还有在使用 GlideDrawable 时,downloadOnly 无法解析
    • 无需将位图发送到另一个屏幕,当您在第一个屏幕上使用滑行时,您将缓存图像,因此在第二个屏幕上它会加载而无需再次加载。 downloadOnly 需要我更改为 .into();。我将更改示例
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多