Send an email with attached file - android

I'm with a problem... When I try to open the Gmail from an Intent, with a attached file, It shows to me in a Toast: "cant send empty file". My PDF file is inside in a folder in my app (/storage/emulated/0/Android/data/teste.com.br.cartaovisitateste/files/business.pdf), but I cant suck this file, to put in the email. Problably, the problem is with to localize the path, but I don't know how to solve this
This is my code:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("vnd.android.cursor.dir/email");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.putExtra(Intent.EXTRA_EMAIL, "wallacekingsdon#gmail.com");
// it does not work
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/Android/data/digitalsa.com.br.cartaovisitaultragaz/files/business.pdf"));
// it does not work too
Uri.fromFile(new File(Environment.getDataDirectory().getAbsolutePath(), "business.pdf"));
i.putExtra(Intent.EXTRA_SUBJECT, String.valueOf(Hawk.get("registro_nome")).concat(" Business Card"));
startActivity(Intent.createChooser(i, "Enviando e-mail..."));

i.setType("vnd.android.cursor.dir/email");
The MIME type of a PDF file is application/pdf. The MIME type of an ACTION_SEND Intent is either the type used for EXTRA_STREAM or EXTRA_TEXT. In your case, you are using EXTRA_STREAM, and that appears to be attempting to point to a PDF file.
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/Android/data/digitalsa.com.br.cartaovisitaultragaz/files/business.pdf"));
Do not hardcode paths. And do not pass things that are not Uri values to Uri.parse(). A Uri has a scheme, like https, file, or content.
Uri.fromFile(new File(Environment.getDataDirectory().getAbsolutePath(), "business.pdf"));
First, you are not putting that in an extra. The value is not being used.
Second, Uri.fromFile() will not work on Android 7.0+ in an Intent extra. You will get a FileUriExposedException. Use FileProvider to serve up the PDF, and use FileProvider.getUriForFile() to get the Uri to put into EXTRA_STREAM, at least on Android 7.0+ devices.

Related

Android Intents - How to share text and image using an Intent to send a sms message in Android

I tried to share a text and attach an image to a sms message using an Intent from my application. It takes the message body but shows error, "unable to attach file" as a toast.
val intent = Intent(Intent.ACTION_SEND)
intent.putExtra("sms_body", message)
intent.putExtra(Intent.EXTRA_STREAM, attachment)
intent.type = "image/*"
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
activity?.startActivity(intent)
I tried lot of ways but had no luck yet.Where do you think i'm wrong? Is it imposible to share images with sms message? Some help is much appreciated.
You are using an android.resource Uri for EXTRA_STREAM. EXTRA_STREAM is supposed to hold a Uri with a content scheme. Not all applications will be able to use your android.resource Uri, because they are not expecting that scheme.
If you wish to improve compatibility, use FileProvider to share your PNG image using FileProvider.getUriForFile(), so you have a content Uri to use with EXTRA_STREAM.

Rename URI file to share audio from raw folder

I'm trying to make an application with sharing of children that are inside the "raw" folder of the application, but I'm not getting it. The file is shared but does not have an .mp3 extension. I was not able to hear Windows Media Player, but I put a .mp3 extension manually. Does anybody have any idea how I do it to get .mp3 extension automatically? Transform / rename a URI into mp3 file.
The name of the song comes as it is registered in the file, I believe that if it was possible to rename adding the .mp3 in the end may work. But I'm not able to rename.
I am using this code:
Intent share;
Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/raw/name_musica");
share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri); share.setType("audio/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Enviar via: "));
The documentation for EXTRA_STREAM states that it is:
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
android.resource: is not content:. There is no requirement for anything that responds to ACTION_SEND to know what to do with an android.resource: Uri in EXTRA_STREAM.
Copy the resource to the local filesystem and use FileProvider, so FileProvider can give you a content: Uri to use with EXTRA_STREAM. Or, write your own ContentProvider that serves the content directly from your raw resource, and use a content: Uri for your provider.
Also, do not use audio/*. Use the actual concrete MIME type of the content that you are sharing.

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 open the gallery using the file path in android

I have designed two buttons.One button for selecting the file and another button for to open the selected file.I have selected the file correctly and the file path also retrieved.But i cant open the particular file directly by the file path.Any one I have tried something like this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ selectedFilePath);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
First, ACTION_GET_CONTENT does not accept a Uri as input.
Second, ACTION_GET_CONTENT has nothing to do with opening a file. Presumably, you should be using ACTION_VIEW.
Third, the value that you are passing to Uri.parse() is not a String form of a Uri.
Also, I would expect few Android devices to have an ACTION_VIEW activity for text/csv content.

How can i send images available in asset folder using intent

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()

Categories

Resources