How to share audio file to multiple apps - android

String sharePath = dataSavingModels.get(pos).getPathOfRecording();
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_TEXT, dataSavingModels.get(pos).getTextSaved());
share.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(share, "Share Sound File"));
This is the code that I am using to send an audio file, it only sends audio files to WhatsApp, and no other app like Messenger, or Telegram or KakaoTalk

We create our own class inheriting FileProvider in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies. First create class with name GenericFileProvider like this:
import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}
Then create provider_paths.xml inside res/xml folder. Folder may be needed to created if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
Now in manifest inside application tag, add this lines:
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
Then Try this code:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".my.package.name.provider", new File(Objects.requireNonNull(sharePath))));
startActivity(Intent.createChooser(share, "Share Sound File"));

Related

Android: Send e-mail via intent with file as attachment

There are many posts on this topic, but I can't find the solution for my problem...
Following: I would like to send a file out of my app via an e-mail attachment.
Sending the file via Whatsapp, save to Google Drive,... works, but not for K-9 Mail or Gmail ("Unable to attache file" Toast message is displayed).
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType("application/zip");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/Download/ExportFile.zip"));
//intentShareFile.putExtra(Intent.EXTRA_TEXT, "message");
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareFile.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(intentShareFile, "Share File"));
I don't understand why it works for all apps, except e-mail apps.
Can anyone help me out?Thanks in advance.
I think your issue is related to the path of the file you are using here (i.e Uri.parse("/sdcard/Download/ExportFile.zip"))
There is a code example to send an email with an attachment on the Android Developers docs here. In the example they pass the Uri as follows:
Uri.parse("content://path/to/email/attachment"). Notice this is called a ContentUri, read more about it form here.
Hope this turns useful for you!
Amr EIZawawy is right. I need to create the Uri of the file and use the File Provider API.
I don't know if I did too much, but this is my solution:
Create a file called "file_paths.xml" in a directory "xml" (create first) within your "res" directory (sibling of layout directory).
The file needs to contain the path to the file you want to share. For the external Download directory this is:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="Download/"/>
</paths>
// The internal path you'd be <files-path name.../> see [FileProvider][1] for all possibilities
Define the File provider within the AndroidManifest.xml with a meta-data link to the just created XML file:
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.<your-app-package-name>.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" /> // Link to the above created file
</provider>
...
Implement the code:
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType("application/zip");
File fileDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File newFile = new File(fileDirectory, "ExportFile.zip");
String authority = "com.bennoss.myergometertrainer.fileprovider"; // Must be the same as in AndroidManifest.
Uri contentUri = FileProvider.getUriForFile(getContext(), authority, newFile);
intentShareFile.putExtra(Intent.EXTRA_STREAM, contentUri);
//intentShareFile.putExtra(Intent.EXTRA_TEXT, "xxx");
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intentShareFile, "Share File"));
For more information see: FileProvider

Android PDF not accepted when sending as Intent.ACTION_SEND

I have a PDF file I am trying to share through Intent.ACTION_SEND. Though when I click on one of the options (gmail, google drive, etc). It says "Request contains no data."
**Updated Code Below
case R.id.share_item:
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, displayedFile.getAbsolutePath());
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share PDF using.."));
return true;
}
I assume the issue must be with my putExtra? That being said the URL for my file definitely works because I can successfully use that path for printing PDFs.
Edit:
I've Updated my code a smidge.
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
Uri uri = Uri.parse(displayedFile.getAbsolutePath());
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share PDF using.."));
This gets me "file format not accepted" from Whatsapp and "Unable to attach file" from Gmail.
Wondering if the issue is with calling from Internal Storage or not using FileProvider.
Edit 2:
Trying to add this code but I am getting a crash
Uri outputPdfUri = FileProvider.getUriForFile(this,
PdfViewActivity.this.getPackageName() + ".provider", sharingFile);
I've also added this to my Manifest and file_paths
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.androidextensiontest.files"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Alright sharing my own answer because I spent hours on this.
case R.id.share_item:
//these policy guidlines do not follow google Guidelines, recommended to change system
//https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
File sharingFile = new File(displayedFile.getPath());
Uri outputPdfUri = FileProvider.getUriForFile(this, PdfViewActivity.this.getPackageName() + ".provider", displayedFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, outputPdfUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//Write Permission might not be necessary
shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share PDF using.."));
return true;
}
This share_item button pulls from my saved file in Internal Storage. They key was getting the manifest and xml file written correctly.
Make sure this goes within your application tags in your AndroidManifest.xml - As well as notice I am using Androidx, change that line accordingly. I also never figured out the android:authorities line, but I found the filled in answer was the most common on Github.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Lastly file_paths.xml - create an xml package inside your red folder, and post this code. For some reason other code was what made my file sharing not work.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="projection" path="." />
</paths>

Android: Cannot share image from SD card

I try to share a picture from my SD card.
Class MainActivity.java
Intent shareIntent = new Intent(Intent.ACTION_SEND);
File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String fileName = "/cats.jpg";
File file = new File(baseDir + fileName);
Uri uri = Uri.fromFile(file);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(shareIntent.createChooser(shareIntent, "Send Picture"));
I add to my Manifest.xml:
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
The image "cats" is in
MySmartphone\Card\DCIM\cats.jpg
When I start my App, I can choose an application to share my picture. But when I open one, it cannot send or open the image.I got often a toast "not supported" or "error to share picture". I think my uri or file is wrong, but I cannot find my mistake. I have no problem to send text.
My uri:
file:///data/user/0/com.example.jana.showpicture/files/cats.jpg
My file: /storage/emulated/0/cats.jpg
Probably you are building for Android API 24 and above. Since API 24 a new kind of security was added. See:
https://developer.android.com/about/versions/nougat/android-7.0-changes.html#perm
You now need to do a bit more, add this in your AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.jana.showpicture.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
/res/xml/provider_paths.xml:
<paths>
<external-path
name="external_files"
path="." />
</paths>
And Java:
Uri uri = FileProvider.getUriForFile(activity,"com.example.jana.showpicture.provider",
file);
shareIntent .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_GRANT_READ_URI_PERMISSION);

Android: How to share images from package directory?

I want to share multiple image from application package directory. My files locate in following path as like. /data/data/com.example. Now i can get images path. But not attach any images to sharing application. as like whats app, Message and Facebook etc. I have implemented permition in manifest file. See below my try.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(saveList.get(0).toString())));
startActivity(Intent.createChooser(intent, "Share"));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/*"); /* This example is sharing jpeg images. */
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : saveList /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
Implemented permission is below.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It is compatible with scoped storage. So don't need any storage permission.
fun shareFile(){
var intentBuilder: ShareCompat.IntentBuilder
intentBuilder = ShareCompat.IntentBuilder.from(this)
.setType("image/jpg")
intentBuilder.addStream(FileProvider.getUriForFile(this.baseContext!!
, getString(R.string.package), File(path)))
val intent = intentBuilder.intent.setAction(Intent.ACTION_SEND).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(intent, "Send to "))
}
In Manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
Create a xml file
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path path="/" name="allfiles" />
<external-path path="/" name="externalfiles" />
<root-path name="external_files" path="/storage/" />
</paths>
First, Uri.fromFile() will not work in your case, because third-party apps have no access to your internal storage.
Second, using a file: Uri, such as from Uri.fromFile(), appears as though it will be banned in the next version of Android.
Instead, you need to use some sort of ContentProvider, such as FileProvider, to make the files available to third-party apps. This is covered in the documentation.

File Sharing in Android

I am new to development with android, i am recently working with an android application that exports document into PDF (conversion) tool , the problem is after exporting of PDF i want to give a option to user to share the document(PDF) through intent, i have digged around stackoverflow but was not able to understand and answers were not actually answering my question. The PDF is exported/created into external sd card.
I have created a PDF through my application after exporting/creating the PDF i want to share them through intent , i have digged around stackoverflow but dint get answer.how can i share it through intent, like i share with image,text through intent.
Answer of user #oleonardomachado is correct but from android N update direct uri share is prohibited. you have to use file provider to get uri data and then share.
Share using intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= 24) {
Uri fileUri = FileProvider.getUriForFile(getContext(), getPackageName()+".fileprovider", file); // provider app name
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
shareIntent.setType("application/pdf");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("application/pdf");
}
startActivity(Intent.createChooser(shareIntent, "Share PDF"));
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"/>
</provider>
</application>
</manifest>
And after that create a file_paths.xml file in res/xml folder. xml Folder may be not there so create if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".").
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Hope this will help others.
You can use ACTION_SEND to activate the chooser for the specified file type, just remember to provide "application/pdf" as the file type.
public void SharePdf(File file) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("application/pdf");
startActivity(Intent.createChooser(shareIntent, "Share PDF"));
}
Now call SharePdf(new File(fileName)) to start the intent and let the user select the correct option.

Categories

Resources