我在 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);
}
有什么想法吗?
我遇到了同样的错误...
我检查了 uri 类型(getActivity().getContentResolver().getType(uri);)...说类型是 image/jpeg,所以有点难住了!IllegalArgumentException:无法使用传递的 URI 来设置壁纸; 检查 ContentProvider 返回的类型是否与 image/* 匹配
这就是我所做的......至少会给它一个在奥利奥上的机会
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);
}
现在我已经做到了
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()
}
Intent.getCropAndSetWallpaperIntent
会在决定是否引发错误之前检查已解析的应用程序。您可以将该方法的实现复制到您的应用程序中并进行相应的修改。最简单的方法是:
try {
startActivity(
Intent(WallpaperManager.ACTION_CROP_AND_SET_WALLPAPER)
.setData(uri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
)
} catch (_: Exception) {
// Ignore
}