How to set the phone wallpaper through an intent - android

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

Related

How to start the "set as" intent (wallpaper, contact picture, etc)

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).

Intent to pick images, but not videos

In my Android app, I use the following code to launch an image picker:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/jpg");
startActivityForResult(intent, INTENT_PICK_IMAGE);
But unfortunately, the system gallery activity that is called by this also display videos. Is there a way to prevent this?
I use this to only allow image types:
intent.setType("image/*");
Edit: setType() is based on MIME type, not file extension (See here: http://developer.android.com/reference/android/content/Intent.html#setType(java.lang.String))

How to access sdcard :

I am writting a program in which user can select one of the text files in the sdcard.
I am using this code to give the user access to sdcard:
in oncreate method:
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "getFile"),
22);
But, I can not access the sdcard. can anybody please help me?
p.s i don't want to use any external library like fileChooser or etc.
You need add a READ_EXTERNAL_STORAGE permission in your AndroidManifest.xml file:
http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

Android - Set Wallpaper using the "Set Wallpaper" intent

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

How to open the My Files folder in Android Programatically (using Intent)?

I am using the below code which opens up the Gallery, Music Player, Dropbox and Contacts, i want the My Files folder to get open programatically, please let me know if there are any specific intent parameters i need to pass to get the File Manager open.
if it is not possible through intent then please give me a snippet or an hint to open the My Files folder programatically.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "View Default File Manager");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
Thanks.
You can use this code to file the files.
int PICKFILE_RESULT_CODE=1;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
this will help you to browse the files from your storage.
Its best that you include a library in your project which handles this scenario.
This worked for me:
This library shows the list of third-party apps. It also has its own file browser for selecting files.
Bad thing is, most Android distributions may or may not ship with a file manager, and even so, may be not with the one which handles CHOOSE_FILE_REQUESTCODE.
So, you are left to create your own file picker activity. Luckily there are many ready made ones available:
http://code.google.com/p/android-filechooser/
https://developers.inkfilepicker.com/docs/android/
If you want to open samsung My Files application try this below code.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);
You have to specifically mention the package name of the explorer application. Please find the example below to open a specific folder in ES Explorer.
public void openfolderInexplorer(String path){
Intent intent = this.getPackageManager().getLaunchIntentForPackage("com.estrongs.android.pop");
if (intent != null) {
// If the application is avilable
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse(path);
intent.setDataAndType(uri, "resource/folder");
this.startActivity(intent);
} else {
// Play store to install app
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" +
"com.estrongs.android.pop"));
this.startActivity(intent);
}
try this below code. if any file manager available , then it will pop up in a form of menu to choose appropriate for the user.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);

Categories

Resources