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?
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:"));
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.
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:"))
And for me work only
Intent intent = new Intent();
intent.setAction(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(ContactsContract.Contacts.CONTENT_URI, "image/*");
intent.putExtra("mimeType", "image/*");
startActivityForResult(Intent.createChooser(intent, "Select service:"), position);
position - it is your : getIntent().getExtras().getInt("id_test");
Related
I have some images saved on the phone internal storage in a filepath that looks like this:
/storage/emulated/0/myApp/FILENAME.jpg
I want to open an intent so that the user can set the phone wallpaper with his app of choice. Every code I tried doesn't work at all or only works with some apps. Thanks.
You can set any image in sdcard or phone by opening this intent:
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(intent, "Select Wallpaper"));
kindly add permission too:
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
for more options check this answer
P.S: You need to add permission for SET_WALLPAPER for android 6+
Update:
You can also open set wallpaper as dialog using :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("jpg", "image/*");
startActivityForResult(Intent.createChooser(intent,
getString(R.string.set_as)), REQUEST_ID_SET_AS_WALLPAPER);
In my app, I download an image from the web and I save it as a jpeg. I want to be able to launch an intent so that the image can be set as a phone wallpaper. The following code works with Google Photo app but not with QuickPic or Nova Launcher, which say errors like "can't open image".
Uri uri = Uri.parse(mFilePath );
Log.v(LOG_TAG, "uri is: " + uri);
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:"));
The URI is always as follows:
/storage/emulated/0/wallpapers/FILENAME.jpg
and the picture gets always correctly saved to the gallery.
I have a bitmap downloaded from a url and want to start android's default set as wallpaper intent/activity passing my bitmap. I have found following solution but missing value of "R.String.set_as" and "REQUEST_ID_SET_AS_WALLPAPER".
https://stackoverflow.com/a/26869604/4767525
what should be the value of this fields?
Thanks
Don't really know about REQUEST_ID_SET_AS_WALLPAPER. I think it is related to the size of the image.
R.String.set_as is just a string: "Set as:"
Use this code:
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(contentUri, "image/*");
intent.putExtra("mimeType", "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(
intent, "Set as:"));
In application user can download file and open it after download completed.
I can open window for choosing application something like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + path);
intent.setDataAndType(uri,"image/*");
context.startActivity(intent);
But here "image/*" is hard-coded. In my application user can download files with different extensions (txt, gif, png, doc, .etc).
Of course I can do something like this:
if(fileExt.equels("png") || fileExt.equels("jpg") || fileExt.equels("gif") )
intent.setDataAndType(uri,"image/*");
But then i need to write code for all extensions =/
So, can you suggest better way to filter this file extensions?
You can use activity chooser: (of course you need to provide MIME type as image/* so that only applications which can handle images will appear in the activity chooser.
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
I searched over the web during the last few weeks (seriously) but I can't find what I need. I just would like to start an intent corresponding to the set as action. It generally offers either Set as wallpaper or Set as contact picture. And then, if more application are installed on the device, they can be listed as well.
Here is an example of what I want :
I precise that I need to support API level 14 and higher.
I found getCropAndSetWallpaperIntent but it works only with content URI which is a problem for me, and is only availbable on API lvl 19 and higher.
I found the answer by my self :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("jpg", "image/*");
startActivityForResult(Intent.createChooser(intent,
getString(R.string.set_as)), REQUEST_ID_SET_AS_WALLPAPER);
You just have to ensure that the uri is public and will be reachable by the crop application chosen by the user.
This solution worked for me with Uri:
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(contentUri, "image/*");
intent.putExtra("mimeType", "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(
intent, "Set as:"));
This worked for me :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//can't use normal URI, because it requires the Uri from file
intent.setDataAndType(Uri.fromFile(new File(uriOfImage)),"image/*");
intent.putExtra("mimeType","image/*");
startActivity(Intent.createChooser(intent,"Set Image"));
You can check that the URI that you pass, should contain the 'file://' prefix (It doesn't work without that).