How can i send images available in asset folder using intent - android

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

Related

Share Google File Type via Intent

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.

Send an email with attached file

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.

How to send the pdf file from the assets folder

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.

Share image puts path in to address

I'm having this issue where sharing an image from my app to Gmail puts the path of the image in the To field.
Here's the code that I'm using:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Beam Dental Insurance Card");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(insuranceCardImageUri, getActivity().getContentResolver().getType(insuranceCardImageUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, insuranceCardImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Insurance Card"));
And here's what I get.
The To: field gets filled in with the path to the image with the "content:" removed from the front. I've tried setting the EXTRA_EMAIL on the intent but that doesn't help.
First, replace:
shareIntent.setDataAndType(insuranceCardImageUri, getActivity().getContentResolver().getType(insuranceCardImageUri));
with:
shareIntent.setType(getActivity().getContentResolver().getType(insuranceCardImageUri));
as ACTION_SEND does not use a Uri in the data field of the Intent.
Then, remove:
shareIntent.setType("image/*");
as you do not need to call setType() twice (or even call setType() and setDataAndType(), as you have it here).
Also, bear in mind:
If the Uri is not coming from your app (e.g., your own ContentProvider), third-party apps like Gmail may not be able to use it, as they may not have permission to access it. This is not significantly different than passing a URL to a third-party app, where the URL requires an authenticated user session to be useful.
There is no requirement for ACTION_SEND implementations to honor both EXTRA_STREAM andEXTRA_TEXT`.
You can share image using share intent, but you've to decode image to a localized Bitmap
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
loadedImage is the loaded bitmap from http://eofdreams.com/data_images/dreams/face/face-03.jpg
acoording to Nitin Misra

Share image with other apps

Here is my problem... I want to share a png image. I have the image in drawable and assets. When I use the sharingIntent it works but not in the way that I want. The file shared appears with numbers and without extension and some apps send the message "unknow file". What can i do??
this is my code:
Uri uri = Uri.parse("android.resource://com.example.shareimages/" + R.drawable.caja);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "send"));
I tried to use instead of drawable files, the files in assets ( with this "file:///android_asset/...) but it didn't work
Try something like this:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");
// For a file in shared storage. For data in private storage, use a ContentProvider.
Uri uri = Uri.fromFile(getFileStreamPath(pathToImage));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(shareIntent)
If you want to share it via the assets folder, you'll have to use a ContentProvider. See this answer on how to do so:
https://stackoverflow.com/a/7177103/1369222

Categories

Resources