getCropAndSetWallpaperIntent() 内容 Uri 错误

Yug*_*agi 2 android android-contentresolver android-contentprovider android-wallpaper

getCropAndSetWallpaperIntent()在android中使用时出现此错误

D/Exception: java.lang.IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/*
Run Code Online (Sandbox Code Playgroud)

但是当我检查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);
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

小智 5

我遇到了同样的错误...

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 更多信息:我单步执行了 getCropAndSetWallpaperIntent() 中的代码,我认为实际错误是某些设备/操作系统版本没有裁剪和设置活动。抛出的异常只是有错误的文本。事实上,查看 API 28 的代码,它似乎甚至没有检查该函数中的 MIME 类型。我的猜测是在奥利奥之前它确实检查了 MIME 类型并抛出异常。但重构后,异常文本从未更新。 (2认同)