Hi It would help me greatly If anyone could please help me out. I have been trying to find a solution to Share my Audio File with a Text using Intent. I am just trying to share it on Whatsapp alone. It would be greatly appreciated if any of you could spare time out to help me out of this problem. When I run my code only the sound is shared. No text.
This is my code:
public void buttonClick(View v) {
try {
String a = copyFiletoExternalStorage(R.raw.accio, "accio.mp3");
String shareBody = "Here is the share content body";
Intent shareMedia = new Intent(Intent.ACTION_SEND);
//set WhatsApp application.
shareMedia.setPackage("com.whatsapp");
shareMedia.setType("*/*");
//set path of media file in ExternalStorage.
shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(a));
shareMedia.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
shareMedia.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(shareMedia, "Compartiendo archivo."));
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Whatsapp no se encuentra instalado", Toast.LENGTH_LONG).show();
}
}
I want the user to share the sound and the text in just one intent.
first you need to add provider_paths.xml
see this video to add file provider
my file provider
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
then add this line to AndroidManifest.xml
<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>
then use this code to share your voice and text
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "some text ... ");
sendIntent.setType("text/plain");
if (hasReadExternalStoragePermission()) {
Uri uriImage = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "voice.wav"));
sendIntent.putExtra(Intent.EXTRA_STREAM, uriImage);
sendIntent.setType("audio/*");
}
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sendIntent, "share"));
I hope it has been helpful
Try to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND
Intent shareMedia = new Intent(Intent.ACTION_SEND_MULTIPLE);
//set WhatsApp application.
shareMedia.setPackage("com.whatsapp");
shareMedia.setType("*/*");
String[] extraMimeTypes = {"audio/*", "image/*"};
shareIntent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
shareIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(a));
shareMedia.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
shareMedia.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(shareMedia, "Compartiendo archivo."));
Related
How can i share a pdf file in android i am able to view the pdf using pdfView library from the abosolute path file so I think the path is correct but am not able to share using that same path that am using to view the pdf, i also try using toast to view the path which gives the correct path but when i try sharing the file the app crashes here is the code am using
Uri uri = Uri.fromFile(new File(arrayList.get(i).getAbsolutePath()));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
activity.startActivity(Intent.createChooser(intent, "Share Via"));
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, "No Sharing App Found", Toast.LENGTH_SHORT).show();
}
``
So Here is the finally solution that worked
First this goes into the manifest just before the closing tag of your application
<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>
Then create an xml resources directory in your res folder then create the file you specified in your manifest that is provider_paths the past this inside the file
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
Then finally in your actual code do this
Uri uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", new File(pdf_list.get(i).getAbsolutePath()));
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
activity.startActivity(Intent.createChooser(intent, "Share Via"));
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, "No Sharing App Found", Toast.LENGTH_SHORT).show();
}
Android App Problems sending an email with multiple attachments using File Provider.
I was using intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+csvFilePath)); and I have no issues sending single attachment file. Then I need to send multiple attachments. I have a problem to get it working.
In my AndroidManifest.xml I specify the provider with the following code :
<provider
android:name="android.support.v4.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>
Here is my xml/provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="myfiles"
path="Android/data/com.example.abc/files/Documents"/>
</paths>
Send email code and files path are :
csvFilePath : /storage/emulated/0/Android/data/com.example.abc/files/Documents/Test123.csv
xyzFilePath : /storage/emulated/0/Android/data/com.example.abc/files/Documents/xyz123.txt
//attach multiple file
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
ArrayList<Uri> uris = new ArrayList<Uri>();
//uris.add(Uri.fromFile(new File(csvFilePath)));
//uris.add(Uri.fromFile(new File(xyzFilePath)));
// using file provider
File csvFile = new File(csvFilePath);
File xyzFile = new File(xyzFilePath);
uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", csvFile ));
uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", xyzFile ));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
intent.setData(Uri.parse("mailto:" + abc#xyz.com));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
Exception raises during sending mailandroid.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND_MULTIPLE dat=mailto:xxx.xxxxxxxx#xxxxx.xxx flg=0x10000001 clip={null U:content://com.example.abc.provider/myfiles/Test123.csv ...} (has extras) }
Found a solution with the following code :
xml/provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
File baseDir = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOCUMENTS), "ABC");
if (!baseDir.exists()) {
baseDir.mkdirs();
}
Log.i("Debug", "baseDir " + baseDir.toString());
File f1 = new File(baseDir, "File1.txt");
writeToFile(f1, "This is file 1 contents 888888888");
Log.i("Debug", "f1 path" + f1.getAbsolutePath());
File f2 = new File(baseDir, "File2.txt");
writeToFile(f2, "This is file 2 contents 123456");
Log.i("Debug", "f2 path" + f2.getAbsolutePath());
String f1path = f1.toString();
String f2path = f2.toString();
string EXTRA_RECIPIENT = "janedoe#abc.com";
String message = "Test message 12345678 this is a test. ";
sendMail(f1path, f2path, message, EXTRA_RECIPIENT);
private void sendMail(String f1path, String f2path, String message, String mailTo) {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo});
File f1new = new File(f1path);
File f2new = new File(f2path);
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", f1new ));
uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", f2new ));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(intent);
}
The gmail or mail sending apps may be disabled in your mobile. Kindly check the apps are enabled.
This ActivityNotFoundException occurs mainly when there is no application to handle the intent.
Then I need to send multiple attachments. I have a problem to get it
working.
Example for multiple attachments:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple");
intent.putExtra(Intent.EXTRA_TEXT, "multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
Finish with a call to startActivity() passing intent.
Using Android FileProvider, when I try to share a file using gmail it fails, I get this gmail toast message: "Couldn't attach file".
It also puts a text string showing the file name and path in the 'To' area of the gmail.
When I try to share a file using Google drive it fails, I get the message:
Upload was unsuccessful
Unable to schedule 1 file for upload.
My question:
Did I miss something in my code? Please provide code with your answer.
XML code:
ANDROIDMANIFEST.XML
<application
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.audiorecorder.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
</application>
res/xml/file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_audio" path="AudioRecorder/"/>
</paths>
Java code:
case R.id.context_menu_share:
/** ***********************************************
* Share Recording feature, code with FileProvider
* ***********************************************
*/
Intent i = new Intent(Intent.ACTION_SEND);
final String rowName2 = (itemNameArray.get(info.position));
File filePath = new File(this.getFilesDir(), "/AudioRecorder/");
File newFile = new File(filePath, rowName2);
Toast.makeText(getApplicationContext(), "newFile = " + newFile, Toast.LENGTH_LONG).show();
Uri uri = FileProvider.getUriForFile(this, "com.mydomain.audiorecorder.fileprovider", newFile);
Toast.makeText(getApplicationContext(), "uri = " + uri, Toast.LENGTH_LONG).show();
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"add#email-address.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"Test");
i.setDataAndType(uri, "audio/wav");
i.putExtra(Intent.EXTRA_STREAM, uri);
this.startActivity(i);
try {
startActivity(Intent.createChooser(i, "Share..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
return true;
I have read all of the similar posts in regards to sending an attachment from internal memory using the GMAIL app. I implemented the content provider suggestion and still cannot get the attachment to actually send using GMAIL.
When passing the intent, everything seems like it is working, the file just gets removed when sent and the recipient sees no attachments. This code works fine on the native Android Email application.
Any suggestions or comments are greatly appreciated.
Android Manifest:
<provider
android:authorities="com.myapp.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"/>
</provider>
file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="/data/user/0/com.myapp/files" path="."/>
</paths>
java:
ShareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
File fileToSend = new File("");
File dir = getFilesDir();
File[] subFiles = dir.listFiles();
if (subFiles != null) {
for (File file : subFiles) {
if (file.getName().equals(curveChosen)) {
fileToSend = file;
}
}
}
File Path = new File(view.getContext().getFilesDir(),"");
File newFile = new File(Path,fileToSend.getName());
Uri contentUri = FileProvider.getUriForFile(view.getContext(),"com.myapp.fileprovider", newFile);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
sendIntent.putExtra(Intent.EXTRA_TEXT,"Email Text");
startActivity(Intent.createChooser(sendIntent,"Select Email Application"));
}
});
I tried with the following code but it is not attaching the pdf file.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");
if (isOnlyWhatsApp) {
sendIntent.setPackage("com.whatsapp");
}
Uri uri = Uri.fromFile(attachment);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
activity.startActivity(sendIntent);
I had this issue where I was trying to open a pdf file from assets folder and I did not work, but when I tried to open from Download folder (for example), it actually worked, and here is an example of it:
File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "example.pdf");
Uri uri = Uri.fromFile(outputFile);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");
activity.startActivity(share);
Please note If your targetSdkVersion is 24 or higher, we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.
Step 1: add a FileProvider tag in AndroidManifest.xml under application tag.
<provider
android:name="android.support.v4.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>
Step 2:
then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to 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=".") with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
step 3: The final step is to change the line of code below in
Uri photoURI = Uri.fromFile(outputFile);
to
Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this, PdfRendererActivity.this.getPackageName() + ".provider", outputFile);
step 4 (optional):
If using an intent to make the system open your file, you may need to add the following line of code:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Hope this will help :)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
pdfUri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", pdfFile);
} else {
pdfUri = Uri.fromFile(pdfFile);
}
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, pdfUri);
startActivity(Intent.createChooser(share, "Share"));
If you are using Intent.createChooser then always open chooser
This works for me kotlin code.
var file =
File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"${invoiceNumber}.pdf"
)
if (file.exists()) {
val uri = if (Build.VERSION.SDK_INT < 24) Uri.fromFile(file) else Uri.parse(file.path)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
type = "application/pdf"
putExtra(Intent.EXTRA_STREAM, uri)
putExtra(
Intent.EXTRA_SUBJECT,
"Purchase Bill..."
)
putExtra(
Intent.EXTRA_TEXT,
"Sharing Bill purchase items..."
)
}
startActivity(Intent.createChooser(shareIntent, "Share Via"))
}
I have used FileProvider because is a better approach.
First you need to add an xml/file_provider_paths resource with your private path configuration.
<paths>
<files-path name="files" path="/"/>
</paths>
Then you need to add the provider in your manifests
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="cu.company.app.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_paths" />
</provider>
and finally in your Kotlin code
fun Context.shareFile(file: File) {
val context = this
val intent = Intent(Intent.ACTION_SEND).apply {
//file type, can be "application/pdf", "text/plain", etc
type = "*/*"
//in my case, I have used FileProvider, thats is a better approach
putExtra(
Intent.EXTRA_STREAM, FileProvider.getUriForFile(
context, "cu.company.app.provider",
file
)
)
//only whatsapp can accept this intente
//this is optional
setPackage("com.whatsapp")
}
try {
startActivity(Intent.createChooser(intent, getString(R.string.share_with)))
} catch (e: Exception) {
Toast.makeText(this, "We can't find WhatsApp", Toast.LENGTH_SHORT).show()
}
}
ACTION_VIEW is for viewing files. ACTION_VIEW will open apps which can handle pdf files in the list.
startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(reportFile), "application/pdf")));
I thought the ACTION_SEND intent would mean "send to other app" and not striktly "send somewhere else".
Try adding Intent.setType as follows:-
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
// sendIntent.setType("text/plain");
if (isOnlyWhatsApp) {
sendIntent.setPackage("com.whatsapp");
}
Uri uri = Uri.fromFile(attachment);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("application/pdf");
activity.startActivity(sendIntent);
For sharing text, below you can find a good example, where you can share text with specific number if you would!
public static void openWhatsAppConversation(Activity activity, String number) {
boolean isWhatsappInstalled = isAppInstalled(activity, "com.whatsapp");
if (isWhatsappInstalled) {
Uri uri = Uri.parse("smsto:" + number);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setPackage("com.whatsapp");
activity.startActivity(sendIntent);
} else {
ToastHelper.show(activity, "WhatsApp is not Installed!");
openMarket(activity, "com.whatsapp");
}
}
Try with following code
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
File pdfFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "Your file");
Uri uri = Uri.fromFile(pdfFile);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share via"));
go to file manager apps in android
and open it
then go to >>>data>>>data>>>com.whatsapp and then >>>share_prefs
open com.whatsapp_preference.xml file
search and select file >>>>name=document pdf ....< /string > and save this file
after >>>setting>>>>apps>>>>whatsapp>>>>and press force stop
new open whatsapp again and try to send or share your document