Intent.ACTION_SEND with image from resource - android

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.

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.

How to share file located in Downloads directory?

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!

Open Folder Using Default File Explorer

I am trying to open "MyFolder" but it is opening recent files folder.
I have tried many techniques, nothing seems to work.
All are similar to this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() +
File.separator + "MyFolder" + File.separator);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
Is it possible, if yes, how can I do it?
Any help would be appreciated.
First, there is no standard Intent action to "open" a filesystem directory on Android.
Second, the value that you pass to Uri.parse() needs to be a string form of a Uri, with a scheme. If you are going to create a Uri from a File, use Uri.fromFile(), rather than turning the File into a String and passing that String to Uri.parse().
Third, ACTION_GET_CONTENT does not take a Uri.

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.

How to attach a text file stored locally in app directory

I have an Android app that saves a text file directly onto the phone, in the app's install directory. I need to allow the user to create a new email, attaching this saved text file. When I start the intent to send the email, everything shows up in Gmail correctly, but the attachment does not get sent. All of my searches on stack overflow seem to only deal with attaching an image file from the SD card. Below is the code that I used. Please let me know if I have done something incorrectly.
File myFile = new File(getFilesDir() + "/" + "someFile.txt");
FileOutputStream stream = null;
if( file != null )
{
steam = openFileOutput("someFile.txt", Context.MODE_WORLD_READABLE);
stream.write(some_data);
Uri uri = Uri.fromFile(myFile);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(Intent.EXTRA_TEXT, email_text);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
file.close();
startActivity(Intent.createChooser(sendIntent, "Email:"));
}
I've also tried sendIntent.setType("application/octet-stream"); but that didn't make a difference. I'm at a loss for why the file doesn't attach and get sent.
Any ideas?
I've just run into exactly the same issue (wanting to attach a text file to a Gmail message). If you look in the Android log, the reason for it is:
02-28 21:01:28.434: E/Gmail(19673): file:// attachment paths must point to file:///mnt/sdcard. Ignoring attachment file:///data/data/com.stephendnicholas.gmailattach/cache/Test.txt
As a workaround, you can use a ContentProvider to provide access to files in your application's internal cache so that Gmail can attach them. I've just written up a blog post on how to do it.
Hopefully that's of some help :)
I've seen this before and the only way I could solve it was by writing the file to the SD card.
It's worth trying writing to the file to the SD card and attaching it if only to eliminate the files location as the cause of the problem.

Categories

Resources