我正在尝试使用 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)
有人可以帮我解决它吗?提前致谢。