使用Glide时如何解决插入图像的错误?

时间:2015-07-28 20:11:00

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

我正在尝试使用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)

有人可以帮我修理吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

你可以尝试的是像这样预取图像

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

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