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.
Related
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 want to share the audio file in android using but didn't found any working solution.
In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).
Also checked that it is creating the file correctly.
I tried to share the audio file with below code;
File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
But while sharing and selecting any app it show unable to attach File.
Also Used FileProvider Class for Nougat Devices but it didn't worked as well.
Checked multiple SO Link as well:
Sharing an audio file
Picking up an audio file android
With regards to the code that you have:
Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri
Use audio/wav, not audio/*, for the MIME type
Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.
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.
I am not able to send a pdf file via email or WhasApp on Android. Below is my code to send the pdf file -- what's wrong with it?
File file = new File("android.resource://beeonline.com.chromatography/raw/vendormumbai.pdf");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sharing Mumbai File...");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Sharing File...");
try {
startActivity(Intent.createChooser(intent, "Share PDF file"));
} catch (ActivityNotFoundException e) {
Toast.makeText(ActivityshowVendorForm.this, "Error: Cannot open or share created PDF report.", Toast.LENGTH_SHORT).show();
}
First, your question title refers to assets. Your code has nothing to do with assets.
Second, android.resource://mypackagename/raw/vendormumbai.pdf is not a file path, just as https://stackoverflow.com/questions/40222995/how-to-send-the-pdf-file-from-the-assets-folder-via-share-option-in-mail-or-wats is not a file path. Your value is a string representation of a Uri, pointing to a raw resource (not an asset).
You are welcome to try replacing:
File file = new File("android.resource://mypackagename/raw/vendormumbai.pdf");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
with:
Uri thing = Uri.parse("android.resource://mypackagename/raw/vendormumbai.pdf");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(thing,"application/pdf");
That should work better, but the android.resource scheme is uncommon, so not all ACTION_SEND recipients will recognize it. Also, this assumes that you are using raw resources, not assets.
If you really want to serve the PDF from assets, or if android.resource is not supported by the apps you want, you can try my StreamProvider, which offers a ContentProvider that, among other things, can serve assets and raw resources using a content Uri.
Or, you can implement your own ContentProvider that can serve the file from assets/ or res/raw/.
Or, you can copy the asset (or raw resource) to a local file (e.g., in getFilesDir() or getCacheDir()), then use FileProvider to serve that local file.
i'm trying to send some images available in asset folder using Intent but it providing error says shared failed try again later please provide me some suggestion.
Here is what i'm doing
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///android_asset/1.jpg"));
startActivity(Intent.createChooser(share, "Share Image!"));
i also trying by using like this
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://com.example.whatsappshare/asset/1.jpg"));
startActivity(Intent.createChooser(share, "Share Image!"));
Both the ways providing same result.
Once you created bitmap as I said earlier, use following snippet to send it as attachment.
String path = Images.Media.insertImage(getContentResolver(), your_bitmap, "title", null);
Uri screenshotUri = Uri.parse(path);
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
emailIntent.setType("image/png");
startActivity(Intent.createChooser(emailIntent, "Send email using"));
Assets directory is private to your app and is not available to other apps. So sharing directly from assets is not possible. You need to copy the file from assets to a public directory in the filesystem and then send the share intent pointing to the public file. Have a look here.
You can't share the file directly, but should be using a content provider to share it.
To avoid writing your own content provider, see cwac-provider
CWAC-Provider: Helping to Make Content Providers Sane
This project offers a StreamProvider, based on Google's FileProvider. Like FileProvider, StreamProvider is designed to serve up files, for reading and writing, through the ContentProvider interface (content:// Uri values). StreamProvider offers:
Serving files from assets and raw resources
Serving files from getExternalFilesDir() and getExternalCacheDir()