在android
中使用getCropAndSetWallpaperIntent()
时出现此错误
D/Exception: java.lang.IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/*
但是当我使用Content
检查ContentResolver
的类型时,我正在
D/CONTENT TYPE:: IS: image/jpeg
那么Wallpaper Manager
为什么会给我内容错误?
以下是我用来获取图片URI的代码
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
tempPath = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
Log.d("URI OF SET IMAGE", tempPath);
ContentResolver cr = this.getContentResolver();
Log.d("CONTENT TYPE: ", "IS: " + cr.getType(Uri.parse(tempPath)));
return Uri.parse(tempPath);
}
有什么想法吗?
答案 0 :(得分:2)
我收到同样的错误......
IllegalArgumentException:无法使用传递的URI设置壁纸; 检查ContentProvider返回的类型是否与image / *
匹配
我检查了uri类型(getActivity()。getContentResolver()。getType(uri);)...说类型是image / jpeg所以有点难过!!
这就是我所做的......至少会给奥利奥一个机会
try {
Intent intent = WallpaperManager.getInstance(getActivity()).getCropAndSetWallpaperIntent(contentUri);
//startActivityForResult to stop the progress bar
startActivityForResult(intent, ACTIVITY_CROP);
} catch (IllegalArgumentException e) {
// Seems to be an Oreo bug - fall back to using the bitmap instead
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), contentUri);
WallpaperManager.getInstance(getActivity()).setBitmap(bitmap);
imageLoadProgress.setVisibility(View.GONE);
}
答案 1 :(得分:1)
遇到同样的问题。似乎与奥利奥有关。 在较旧版本的Android上运行正常。
现在我已经完成了这个
try {
val intent = manager.getCropAndSetWallpaperIntent((Uri.parse(path)))
}
catch (e: Throwable) {
AlertDialog.Builder(this)
.setTitle("Oops")
.setMessage("${e.localizedMessage}\n\nThe photo has been saved to your Pictures folder. Try setting it as wallpaper manually.")
.setPositiveButton("OK", null)
.show()
}