2

I am getting this error when using getCropAndSetWallpaperIntent() in android

D/Exception: java.lang.IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/*

But when I check for the type of Content using ContentResolver I am getting

D/CONTENT TYPE:: IS: image/jpeg

then why is Wallpaper Manager is giving me content error ?

Here is the code I am using to get Image 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);
}

Any ideas ?

Yugansh Tyagi
  • 646
  • 10
  • 24

2 Answers2

7

I'm getting the same error...

IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/*

I've checked the uri type (getActivity().getContentResolver().getType(uri);)... says the type is image/jpeg so kinda stumped!!

This is what I've done... will at least give it a chance on Oreo

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);
}
Carc.me
  • 124
  • 1
  • 6
  • 2
    A little more info: I stepped through the code in getCropAndSetWallpaperIntent(), and I think the actual error is that some devices/OS Versions don't have a crop and set Activity. The exception that is being thrown simply has the wrong text. In fact, looking at the code for API 28, it doesn't appear to ever even check the MIME type in that function. My guess is pre-oreo it did actually check MIME type and throw. But after a refactor, the exception text was never updated. – Adam Oct 03 '19 at 16:36
  • @Adam I am targeting API 29 and getCropAndSetWallpaperIntent() is failing on certain devices with the same shitty message. And the code is still not checking any MIME type but still giving this error. Also, i tried to put in MIME type with the intent which was "image/jpeg" extracted from uri using the given methods, to my surprise getCropAndSetWallpaperIntent() stopped working on devices which was already working. So now i have used above solution for now. Android is.... – beginner Aug 30 '20 at 12:49
1

Having the same issue. It seems it is Oreo related. On older versions of Android works fine.

For now I've done this

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()
}
alexbuga
  • 21
  • 4