0

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

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

4

1 回答 1

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

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

于 2015-07-28T20:39:41.287 回答