Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Set Wallpaper using the "Set Wallpaper" intent

Tags:

android

The question was asked here and here but there was no real answer.

Android has a built-in "Set Wallpaper" feature, such feature is available when starting an activity intent with mime "image/jpeg" or long-tapping on images in browser.

My question is: how do I programmatically invoke the built-in "Set Wallpaper" feature using a file Uri?

like image 503
Jeremy Avatar asked Mar 06 '14 02:03

Jeremy


3 Answers

Seems like there is no answer to the question however I did discover a workaround:

    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/jpeg");
    intent.putExtra("mimeType", "image/jpeg");
    this.startActivity(Intent.createChooser(intent, "Set as:"));
like image 149
Jeremy Avatar answered Jan 01 '23 12:01

Jeremy


For me work only:

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

If mime "image/jpeg" apps not found image.

like image 31
stalkerg Avatar answered Jan 01 '23 11:01

stalkerg


If your app crashes after choosing the application you want to set wallpaper with, you need to add

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

A typical example is attachments in a email app. Access to the emails should be protected by permissions, since this is sensitive user data. However, if a URI to an image attachment is given to an image viewer, that image viewer no longer has permission to open the attachment since it has no reason to hold a permission to access all email.

The solution to this problem is per-URI permissions: when starting an activity or returning a result to an activity, the caller can set Intent.FLAG_GRANT_READ_URI_PERMISSION and/or Intent.FLAG_GRANT_WRITE_URI_PERMISSION. This grants the receiving activity permission access the specific data URI in the intent, regardless of whether it has any permission to access data in the content provider corresponding to the intent. https://developer.android.com/guide/topics/permissions/overview

val intent = Intent(Intent.ACTION_ATTACH_DATA)
        .apply {
            addCategory(Intent.CATEGORY_DEFAULT)
            setDataAndType(uri, "image/*")
            putExtra("mimeType", "image/*")
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
    startActivity(Intent.createChooser(intent, "Set as:"))
like image 42
Michal Engel Avatar answered Jan 01 '23 11:01

Michal Engel