I have implemented the following code to share video:
// Copy video file to Fileprovider directory.
final String destFile = …
// Build FileProvider uri.
final Uri uri = FileProvider.getUriForFile(activity, AUTHORITY, destFile)
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("video/*");
Log.d(TAG, "Sharing " + sendIntent.getType() + " for " + uri);
String title = "Share this content with";
Intent chooser = Intent.createChooser(sendIntent, title);
if (null != sendIntent.resolveActivity(activity.getPackageManager())) {
activity.startActivity(chooser);
}
This code works and shows the chooser. When I select a messaging app like Telegram, I get to pick the destination conversation, but the video is sent as a file attachment, meaning that users only see a document icon and need to download it and open externally. However, the same video, using the OS system gallery, will open Telegram into their video editor where they can cut/edit the media and when sent will be visible inline in the conversation.
What am I missing to get the same behaviour? Looking through Android git repositories I don't see anything different from this to share content, so I don't know what I'm missing. The log I get with this code looks like
Sharing video/* for content://com.app.android.fileprovider/share/video-a.mp4
So not only does it have the mime type but also the file extension could help. When I change the code to use a different mime type for images then I get the same behaviour, where the images get inlined into Telegram's chat.
For some reason the culprit was the FileProvider. Once I removed the FileProvider and passed in directly file:// URIs everything works.
Related
I was trying to create a chooser to grant users freedom to open a pdf file with the application they prefer.
Here's my approach:
private void openPdf(String pdfUrl, String mime) {
// Intent pdfViewIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Intent pdfViewIntent = new Intent(Intent.ACTION_VIEW);
pdfViewIntent.setDataAndType(Uri.parse(pdfUrl), mime);
Log.d(TAG, "openPdf: uri: " + Uri.parse((pdfUrl)));
Intent chooser = Intent.createChooser(pdfViewIntent, "Choose an app");
// chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // optional
startActivity(chooser);
finish();
}
The pdf url is something like this: https://drive.google.com/file/d/.../view?usp=sharing
And for the mime type, I tried using both "application/vnd.google-apps.document" and "application/pdf".
Now, when the user clicks on document, I get an output like the following:
But I want an output like the following screenshot: (it appears when I try to open the same link with WhatsApp)
NOTE 1: using WebView is not a good option for me.
NOTE 2: the above approach works if the URL is in the format https://example.com/.../filename.pdf. But it fails for Google Drive's link.
Is there any way to create a share intent that gives the user the option to save the file somewhere in the device? Like an intent with ACTION_CREATE_DOCUMENT.
My situation is the following: I have a file that is NOT visible to the user, saved in the app's documents folder via getExternalFilesDir, and I want to allow the user to either share that file, or save it directly in an accessible folder (it would be nice to suggest the user a path, like the file explorer opening directly in the public Download folder, but it is not needed). All of this should be wrapped inside the share intent, as I am only allowed to edit that as of now.
The code looks something like this:
String packageName = Utils.currentContext.getApplicationContext().getPackageName();
Uri uri = FileProvider.getUriForFile(Utils.currentContext, packageName + ".provider", file);
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType(type);
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, text);
intentShareFile.putExtra(Intent.EXTRA_TEXT, text);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent shareIntent = Intent.createChooser(intentShareFile, "Sharing file");
Utils.currentActivity.startActivity(shareIntent);
I tried working with putExtra as well as addCategory with no success.
I am trying to share files from my app to another using the android app picker but after choosing the recieving app, say, whatsapp I get a "format not compatible" error once inside the whatsapp app.
The file I intend to share is always located in the DOWNLOADS folder.
My code for sharing is:
private void share() {
Uri uri = FileProvider.getUriForFile(getContext(), getContext().getResources().getString(R.string.file_provider_authority),
new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + fileToShare.getFileName()));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, fileToShare.getFileName()));
}
I know I need to use FileProviders because I'm targeting above V24 and believe this is the right way to do it but something must be missing because I keep getting this error.
Can you tell me what am I doing wrong so i can fix my code? Thank you!
I'm trying to share Google File Type (eg: application/vnd.google-apps.document, application/vnd.google-apps.form, application/vnd.google-apps.presentation, application/vnd.google-apps.spreadsheet) with the Intent.
My code:
private void shareFile(String uri) {
ContentResolver contentResolve = getContentResolver();
String mimeType = contentResolve.getType(Uri.parse(uri));
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse(uri));
intentShareFile.setType(mimeType);
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Hey, check out this file!");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
But when I click an apps to share that file, it doesn't respond and not do anything. Even when I'm trying to upload it on Google Drive, it says that upload is unsuccessful. The share file works fine when I share pdf or docx file.
Apparently, we can't share the Google File Type via Intent. We have to convert the file that has Google File Type to something like Microsoft File Type (application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/msword, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/x-excel, application/vnd.ms-powerpoint, or application/vnd.openxmlformats-officedocument.presentationml.presentation).
Is there any way to convert the URI mimeType so I can share a file that has Google File Type? For the example is changing a file that has application/vnd.google-apps.document as its mimeType to application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation, and etc. Is there a way to do that so I can share a file that has Google File Type? Any help will be appreciated, thanks!
Do it like this:
Intent uploadIntent = ShareCompat.IntentBuilder.from(this)
.setType(mimeType)
.setStream(fileUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(uploadIntent)
Please note setStream requires Uri.
I need to perform a realy simple thing - share an image with text.
What I found to be the best is the following
final Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, context
.getString(R.string.fragment_etc_share_text));
sendIntent.setType("image/jpeg");
final Uri imageUri = Uri.parse("android.resource://"
+ getActivity().getPackageName() + "/"
+ R.drawable.share_attachment);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sendIntent);
share_attachment is an .jpg picture.
It works fine except for the image extension. I got something like "23244232" in the attachment. If I download the file and add an ".jpg" to its name - it opens well. But without the extension many social networks don't send it.
I tried to use assets but had no success as it's not a file actually on the runtime.
Using an external storage is not an option because the app must work even on old devices without SD card and be stable on USB plug in and out.