Yugansh Tyagi
Yugansh Tyagi

Reputation: 656

getCropAndSetWallpaperIntent() Content Uri Error

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 ?

Upvotes: 2

Views: 1047

Answers (3)

Dewey Reed
Dewey Reed

Reputation: 4956

It could be related to the package visibility because Intent.getCropAndSetWallpaperIntent checks resolved apps before deciding if it throws an error.

You can copy the implementation of the method into your app and modify it accordingly. The simplest way is:

try {
    startActivity(
        Intent(WallpaperManager.ACTION_CROP_AND_SET_WALLPAPER)
            .setData(uri)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    )
} catch (_: Exception) {
    // Ignore
}

Upvotes: 0

Carc.me
Carc.me

Reputation: 124

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);
}

Upvotes: 7

alexbuga
alexbuga

Reputation: 21

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()
}

Upvotes: 1

Related Questions