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.
Related
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).
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");
In my app, I am opening my image using this code:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(picPathUriString)), "image/png");
startActivity(intent);
I run it on S2 and it opens the picture using the gallery as expected and showing all the capabilities of that gallery app (such as "Share" and "Send" ..etc)
On the S4, it shows the picture but the above options such as SHARE are not there. Although the Gallery on S4 has all the options if opened seperately
WHYYY? How can I make sure I get all the options?
Thanks
Try this....
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Uri uri = Uri.parse(Uri.parse(picPathUriString));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share via"));
I'm looking to display a specific folder of images in an app so that the user can browse through them.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("content://sdcard/Pictures/Album"), "image/*");
startActivityForResult(intent, 0);
This seems to display all photos on the phone instead of the images I have in this album. How can I change this code so that the user can browse through images in a specific folder using the gallery app? The idea behind this is not so that the user can choose an image like I've seen in many examples on here but just simply to browse the images.
you can try this
File root = new File(Environment.getExternalStorageDirectory().getPath()
+ "/myFolder/");
Uri uri = Uri.fromFile(root);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(uri);
startActivityForResult(intent, 1);
I would like to open just one image and have pitch-zoom function, so (as I think) easiest way to do that is to open that image in gallery intent. Anybody can share example of how to call gallery intent with particular picture from drawable?
I tried to used something like that but can't make it working (wrong file patch?).
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("com.me.example/drawable/thisimage.png"), "image/*");
startActivity(intent);
According to this post
You should do something like:
String uriStr = "android.resource://"+ "com.example.myapp"+ "/" + R.drawable.photo_h01;
Uri uri = Uri.parse(uriStr);
I have not Eclipse in front of me but that's what I have found on several sources