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;
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();
}
I wish to open a file from downloads folder (/storage/emulated/0/Download) and this is my code
I first check if the file exists
File file = new File("/storage/emulated/0/Download" + File.separator + <file-name> + ".pdf");
if(file.exists()) {
openfile("/storage/emulated/0/Download" + File.separator + <file-name> + ".pdf");
Log.d("upload", "onClick: already exists");
}
else {
downloadfile(Url);
Log.d("upload", "onClick: download");
}
If the file exists I use this code to open the file
public void openfile(String path){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID+".provider",new File(path));
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.EXTRA_STREAM,uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(intent,"Choose an application"));
}
This is my 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/path"/>
</provider>
and my path.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>
When I click to open the file it shows intent chooser and when drive pdf viewer is selected it opens up with a blank screen and closes immediately and when any other app is chosen then it shows error File not found(content://(package-name).provider/storage%2Femulated%2F0/Download/file-name.pdf). I don't know what I am doing wrong.
maybe not
intent.addFlags(Intent.EXTRA_STREAM,uri);
but
intent.putExtra(Intent.EXTRA_STREAM, uri)
?
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 thoroughly checked that pdf file is
in
/storage/emulated/0/Download/Abcd.pdf"
but can't open it with intent.
I opened it in various viewers and some oh them result in a error: "can't open file". Microsoft word says: check file in the device, but Abcd.pdf file is opened well when I opened it in file directory in file system of android.
Did I set the route wrong?
AndroidManifest.xml
<provider
android:authorities="${applicationId}.provider"
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/provider_paths"/>
</provider>
MainActivity.Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent();
File mypath=new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/","Abcd.pdf");
Log.e("download",mypath.getAbsolutePath());
//this log says : /storage/emulated/0/Download/Abcd.pdf
Uri pdfUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", mypath);
Log.e("download",mypath.exists()+"");
if (mypath.exists()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.e("download","result : "+pdfUri.getPath());
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
intent.putExtra(MediaStore.EXTRA_OUTPUT, pdfUri);
}else{
}
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Log.e("error","error"+e);
}
}
}
res/xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="storage/emulated/0"
path="."/>
</paths>
I am using this function :
public void openPdf(LocalFile magazine) {
if (BuildConfig.DEBUG)
Log.d(TAG, "openPdf() called with: magazine = [" + magazine + "]");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
File file = new File(Uri.parse(magazine.getPath()).getPath());
Uri uri = FileProvider.getUriForFile(getContext(),
getContext().getApplicationContext().getPackageName() + ".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(Uri.parse(magazine.getPath()), "application/pdf");
}
try {
startActivity(intent);
} catch (Throwable t) {
t.printStackTrace();
//attemptInstallViewer();
}
}
This is works fine for me.
I've one question by which this problem might be resolved, Have you wrote Storage Permission in Manifest?
Also is your App has Permission to Read External Storage? Check this from Mobile Settings and it would start working automatically.
Here is how you can initialize Permissions Statement in Manifest File.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If it's given in Manifest and also your Mobile has guaranteed the Permissions and App is not working then share the full code of Activity, I might help you in that.
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."));