How to share file located in Downloads directory? - android

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!

Related

Create a share intent that also allows to save a document on Android

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.

Whatsapp sharing audio fails - "please try again"

I'm using this code for some years now and it worked fine:
final Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("audio/mpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM,
SoundProvider.getUriForSound(getContext(), sound));
getActivity()
.startActivity(Intent.createChooser(sharingIntent,
getContext().getString(R.string.share)));
My SoundProvider generates a URI that starts with content:// which is picked up by a FileProvider (actually the same SoundProvider). This provider reads an audio file from my raw folder.
The sounds was playable directly in WhatsApp (and not a generic file) and shown with the correct title from the ID3 tags.
This has worked flawlessly and still does with Telegram/Dropbox etc. but up until a recent WhatsApp update from a few months ago it fails with the message "Sharing failed please try again".
Is anyone aware of any changes made by WhatsApp and has encountered something similar?
Try this:
Uri uri = Uri.parse(audioPath);
Intent shareIntent = new Intent();
shareIntent.setType("audio/*");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
I had to work around this by copying the sounds to the external-files-dir.
I don't know why whatsapp suddenly doesn't accept files from the raw directory served by a FileProvider anymore while other apps still do without any problems.

Why does ACTION_SEND not behave like gallery's share button?

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.

Facebook Upload of photos fails from Android Chooser

In my app, I have an option to share photos using standard Android sharing mechanism. When I choose gmail or yahoo mail, the sharing works, the images are displayed in the body of an email and are sent to the destination. When I choose Facebook, the images are displayed inside the Facebook activity where you can add a message, but when I hit the Done button, the upload process starts, but always fails.
The title of the message box is: "Facebook Upload Failed" and the message itself is: "Your upload could not be completed because we could not access your photo(s)."
Here is the code snippet for sharing:
mShareIntent = new Intent();
mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
mShareIntent.setType("image/jpeg");
mShareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ArrayList<Uri> uris = new ArrayList<Uri>();
for(int i = 0; i < mImageUrls.size(); i++) {
uris.add(Uri.parse("file://"+mShareFiles[i].getAbsolutePath()));
}
mShareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(mShareIntent, "Share Images To..."))
The photos are obviously accessible, since they are successfully processed by gmail and yahoo mail. And Facebook, itself, can access them, but not sent them.
As an experiment, I shared photos from the Android Gallery, in the Chooser, I chose Facebook, and the photos were successfully uploaded.
Does anyone have any idea what might be going on?
Thank You,
Gary
If the file is in your app's private directory (or even in its sd card directory in KitKat) then you might run into sharing issues. And FileProvider has its problems too.
The only reliable way I've found to share image files is to copy them to the external storage directory for pictures and sharing from there (you can create a .nomedia subdirectory to avoid them appearing in the Gallery app).
For example:
Bitmap bitmapToShare = ...
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File pictureStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File noMedia = new File(pictureStorage, ".nomedia");
if (!noMedia.exists())
noMedia.mkdirs();
File file = new File(noMedia, "shared_image.png");
saveBitmapAsFile(bitmapToShare, file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/png");
startActivity(shareIntent);
}

Intent.ACTION_SEND with image from resource

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.

Categories

Resources