0

I m trying to set image as wallpaper from stored in drawable folder with default android way like this enter image description here

I have tried as answered in other questions here.

But i m getting error as "No apps can perform this action"

My code to get uri-

Uri uri=Uri.parse("android.resource://drawable/sharingan.jpg");

Intent intent=new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri,"image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("mimeType","image/*");
startActivity(Intent.createChooser(intent,"Set as"));

Is there another way to achieve this? Or is there something wrong with my uri ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • can you see if uri is null or not? – Swayangjit Nov 09 '19 at 16:02
  • Possible duplicate of [Android - Set Wallpaper using the "Set Wallpaper" intent](https://stackoverflow.com/questions/22213878/android-set-wallpaper-using-the-set-wallpaper-intent) – Edric Nov 09 '19 at 16:28
  • Could you try setting the MIME type to be of `image/jpeg` instead of `image/*`, as other answers have suggested in the duplicate? You're also adding a redundant extra to the intent (`intent.putExtra("mimeType", "image/*")`). – Edric Nov 09 '19 at 16:30

1 Answers1

0

Construct the uri object like following

Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
 "://" + getResources().getResourcePackageName(R.drawable.sharingan)
 + '/' + getResources().getResourceTypeName(R.drawable.sharingan) + '/' + getResources().getResourceEntryName(R.drawable.sharingan) );
Swayangjit
  • 1,875
  • 2
  • 13
  • 22