I'm developing an app which can send an image to other apps. After Nougat was released we should use FileProvider. But I noticed that this approach is not universal. For earlier versions of Android we should use Uri.getUriFromFile() method. But using FileProvider also doesn't work on Nougat for some apps (i.e. default messages app). Is there any universal method which will work with all apps, or I should define apps which can't work with FileProvider only on practice?
Following code must work properly:
File file = new File(filePath);
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.fileprovider", file);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Share image");
}
Related
I am downloading image and taking it filepath to share but it is not working on all platforms except whatsapp. While it is showing can't upload image on insta and file not attached on gmail.
`private void genericShare(Activity activity) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, downloadedFilesPath);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_TEXT,getClipBoardText(activity));
activity.startActivity(Intent.createChooser(shareIntent,
activity.getResources().getText(R.string.share_using)));
}`
Check the following demo app code, after clone it just change the hardcoded file path.
https://github.com/nikkulshrestha/imageShare
In my app I need to share *.json file to other app (messenger, google disk, etc). How can I do this via Intent or something else?
But when I trying to do this via Intent, I have some problems.
override fun shareBackupData(path: String) {
val uri = Uri.parse(path)
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type = "*/*"
startActivity(Intent.createChooser(shareIntent, "Choose"))
}
When I run this code, I choose app to share and then I see toast "unsupported attachment"
I had similar problems with this and I found this article where it recommends us to use FileProvider.
What it does is :
FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.
I recommend you to take a look to the article and also if you want code, take a look at this Stackoverflow post
i think you can use the file as ExtrtaStream the following code is sharing image file you can change it to your json file
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "foo.jpg");//change it with your file
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using"));
This question already has answers here:
photo share intent in android
(3 answers)
Closed 6 years ago.
I'm having troubles sharing an image from my app to other apps.
The problem is when I share the image, it doesn't include the extension and other apps like messenger, whatsapp or gmail doesn't recognise them.
My code is:
Intent intent = new Intent(Intent.ACTION_SEND);
String path = "android.resource://com.xxxx.xxxxxxx/drawable/image";
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_TEXT, "Hello world!");
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share:"));
I'm having the same problem sharing audio:
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
String sharePath = "android.resource://com.xxx.xxxxx/raw/" + sound;
Uri uri = Uri.parse(sharePath);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("audio/mp3");
Intent i = Intent.createChooser(share, "Share Sound File");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
baseContext.startActivity(i);
I already checked the official documentation and still having problems: https://developer.android.com/training/sharing/send.html
Thanks for help or any hints!
You have to write the image or audio file to the sdcard or ExternalStorege before sharing. The reason is other apps don't have the access to the files which are in your app's private memory. So to share the image or any other file from your app, you must make it available for other apps to read.
I recently noticed that when sharing an animated .gif that I generate from my app, the official Twitter app will only share it as a static image.
Previously, I thought everything was working because when I shared the same type of animated .gif with an unofficial Twitter app (Tweetcaster), it properly shared it as an animated .gif.
The thing I'm questioning is whether this is an issue on my end or with the official Twitter app? I ask this because when sharing an animated .gif from the Android Gallery with the official Twitter app, it is also posted as a static .gif.
Here's the code in question:
public void shareGif(String path)
{
File file = new File(path);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/gif");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "text");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Choose a sharing option"));
}
I was having the same issue as you. I was loading URIs from the drawables and for whatever it was not animating.
I fixed my problem by implementing a custom file provider and loaded GIFs from the assets folder.
Also it was very important to add this flag intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Twitter supports looping GIFs. If your animated GIF doesn’t loop and plays a single time, it will display as a static image.
Hi I'm working on an app where I need to list all types of applications able to open a file. If the file were an image I would do something like this normally to show all applications capable of viewing the image.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*");
context.startActivity(intent);
However say I have an unknown file how could I list all applications able to view a file so the user can select the appropriate application to open the file in my application.
I also need the titles listed in an arraylist if possible so I can list them in a listview.
Thank you for any help with getting a list of applications capable of viewing a file
===================================
Edit
Alright well something easier how can i get the applications from the above intent into an arraylist i could just do image/* audio/* etc and add them all to a list and then list them in a listview and that would solve my problem
As far as i know there is no API in android that links files to a program like in windows. Your best choose I'm afraid is to build a database of known file types and program/android app linked to them.
Okay well i figured my problem out what i was looking for was
Intent audioIntent = new Intent(android.content.Intent.ACTION_VIEW);
audioIntent.setDataAndType(Uri.fromFile(Opener.file), "audio/*");
List<ResolveInfo> audio = packageManager.queryIntentActivities(audioIntent, 0);
for (ResolveInfo info : audio){
String label = info.loadLabel(packageManager).toString();
Drawable icon = info.loadIcon(packageManager);
String packageName = info.activityInfo.packageName;
String name = info.activityInfo.name;
iconlabel.add(a.new HolderObject(label, icon, audioIntent, "audio/*", packageName, name));
}
But the main thing in the code above is the queryIntentActivities method that was what solved my issue allowing me to add those apps to a list
This may help you to list the application that are capable of opening a particular file.
// extension : The file extension (.pdf, .docx)
String extension = filePath.substring(filePath.lastIndexOf(".") + 1);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (null == mimeType) {
// Need to set the mimetype for files whose mimetype is not understood by android MimeTypeMap.
mimeType = "";
}
File file = new File(filePath);
Uri data = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(data, mimeType);
context.startActivity(Intent.createChooser(intent, "Complete action using"));