Sharing a file with Android Intent: File extension is removed - java

I'm using the following code to share an audio file with Android Studio 3.3.2 and Java. The file is extracted from the /raw/ folder.
public void funktionTeilen(String file) {
Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/raw/" + file);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mpeg3");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Audio teilen"));
}
In principle the sharing works fine, but the file is sent without a file extension, which of course makes it unreadable by most apps. If I add the file extension manually (for example after downloading it from the e-mail client), the MP3 works fine.
"file" is fed in by another function and corresponds to the file name from the raw folder. It is also found basically, otherwise the sharing process would not work at all.
So, how can I share a file with retention of the file extension? Thanks for your help!
Update #1
public void funktionTeilen(String Datei) {
try {
File tmpFile = new File(this.getCacheDir() + "/tmpfile.mp3");
InputStream in = getResources().openRawResource(R.raw.meise1);
FileOutputStream out = new FileOutputStream(tmpFile, true);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
// Uri uri = FileProvider.getUriForFile(this, this.getPackageName(), tmpFile);
Uri uri = Uri.fromFile(tmpFile);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Audio teilen"));
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}

I'm using the following code to share an audio file with Android Studio 3.3.2 and Java
You are sharing a resource. That is a file on your development machine. It is not a file on the device.
In principle the sharing works fine, but the file is sent without a file extension, which of course makes it unreadable by most apps.
In Android, resources do not have file extensions when we work with them using the Android SDK.
So, how can I share a file with retention of the file extension?
If you share an actual file, using FileProvider, it will have a file extension. So, copy the bytes corresponding to your resource to a file (e.g., in getCacheDir()), then set up FileProvider and use FileProvider.getUriForFile() to get the Uri to use with EXTRA_STREAM.

I worked out the solution!
MainActivity.java
public void funktionTeilen(String Datei) {
try {
File tmpFile = new File(this.getCacheDir() + "/tmpfile.mp3");
InputStream in = getResources().openRawResource(R.raw.meise1);
FileOutputStream out = new FileOutputStream(tmpFile, false);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
/* if (tmpFile.exists()) {
Toast.makeText(MainActivity.this, tmpFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
} */
}
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, tmpFile.getAbsoluteFile());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mpeg3");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Audio teilen"));
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<application>
<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_paths"/>
</provider>
</application>
file_provider_paths.xml
<paths>
<cache-path name="cache" path="/" />
<files-path name="files" path="/" />
</paths>

Related

Why does it crash while sending Bitmap

I am trying to share an image by sending a Bitmap. The solution i found works on the emulator for Android 10.0 but not on Android 7.0 (neither on my LG V20 running Android 9) where it crashes.
Here is my current code. Any idea how to avoid the crash? Thanks in advance for your help.
picIV.buildDrawingCache();
Bitmap bitmap = picIV.getDrawingCache();
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, "Challengers", null);
Uri uri = Uri.parse(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
//share.putExtra(Intent.EXTRA_TEXT, "I found something cool!");
startActivity(Intent.createChooser(share, "Share Your Design!"));
The crash seems to be happenning when running this line
Uri uri = Uri.parse(path);
Maybe your path is null? Did you debug it?
From the source of the method Uri.parse(...) I noticed that it returns an error if it takes null as an argument and MediaStore.Images.Media.insertImage(...) returns null "if the image failed to be stored for any reason".
Found another method that worked on Android 7 but not on Android 10. So found out a way to make them work together.
Uri uri;
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Challengers", null);
if (path!=null) {
uri = Uri.parse(path);
}else{
uri = null;
String string="" ;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "to-share.png");
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
stream.close();
uri = Uri.fromFile(file);
} catch (IOException e) {
Log.d(string, "IOException while trying to write file for sharing: " + e.getMessage());
}
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
//share.putExtra(Intent.EXTRA_TEXT, "I found something cool!");
startActivity(Intent.createChooser(share, "Share Your Design!"));
This should work across each API -- Answer is in Koltin
Create Bitmap From View
fun createBitmapFromView(view: View): Bitmap {
val bitmap = Bitmap.createBitmap(view.measuredWidth,
view.measuredHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.layout(view.left, view.top, view.right, view.bottom)
view.draw(canvas)
return bitmap
}
Create a File for saving the Bitmap
fun getFileFromBitmap(bitmap: Bitmap): File? {
val parentDirectory = File(externalCacheDir, "reminder_images") //image file is stored in external cache directory/reminder_images
if (!parentDirectory.exists()) {
parentDirectory.mkdirs()
}
val externalCacheFile = File(parentDirectory, "name_of_your_file.png")
return writeBitmapToFile(externalCacheFile, bitmap)
}
Write the Bitmap to File using Stream
fun writeBitmapToFile(externalCacheFile: File, bitmap: Bitmap): File? {
return try {
val fOut = FileOutputStream(externalCacheFile)
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut)
fOut.flush()
fOut.close()
externalCacheFile
} catch (e: Exception) {
null
}
}
Create the FileProvider in Manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.your_package_name.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
5.Create the provider_paths.xml resource file
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Get Uri From File using FileProvider
val uri = FileProvider.getUriForFile(context,"com.your_package_name.FileProvider", externalCacheFile)
Once you have the Uri of the image file share the file as you were doing earlier using Intent.
In my solution, the image file is stored in app-specific external directories. This image file would be deleted if your app is uninstalled. If you want some other behaviour please check data-storage.
Finally found the problem. It was the permissions. Even though i had put the permissions in the Manifest.xml also need to add a couple lines in onCreate.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
onRequestPermissionsResult(requestCode,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},grantResults);
Thanks everyone for your answers, really helpful!

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/

EDIT:Ok I have tried the suggestions and changed it to getExternalFilesDir() and am still getting the same error. Skip to the bottom where it says "EDITED CODE" to see what it is now. I also changed it so that the screenshot will save to the pictures directory instead of creating a new directory. (END EDIT)
I have an android app that contains a recyclerview. I have created a button that will export create a PNG of the recyclerview data, save it to the device, and then send it as an attachment to the email application so it can be emailed. I'm getting the exception " java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/ExportedFlares/FlareData.png"
Here is the code for the function that saves the bitmap to the device:
private void saveBitmap(Bitmap bitmap){
if(bitmap!=null){
try {
FileOutputStream outputStream = null;
try {
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ExportedFlares";
File dir = new File(file_path, "FlareData");
if(!dir.exists())
dir.mkdirs();
outputStream = new FileOutputStream(dir); //here is set your file path where you want to save or also here you can set file object directly
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); // bitmap is your Bitmap instance, if you want to compress it you can compress reduce percentage
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And here is the onClick code for when the button is tapped:
public void onClick(View v) {
saveBitmap(getScreenshotFromRecyclerView(recyclerView));
String filename = "FlareData.png";
File fileLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ExportedFlares", filename);
Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"email#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
The final line of the following code is what is throwing the exception:
File fileLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ExportedFlares", filename);
Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);
Here is the XML data, I have provider_paths.xml here:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="FlareData.png" path="ExportedFlares/"/>
</paths>
And this is from the manifest:
enter code here
<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>
EDITED CODE:
emailFlaresButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = getApplicationContext();
saveBitmap(context, getScreenshotFromRecyclerView(recyclerView));
String filename = "FlareData.png";
File fileLocation = new File(context.getExternalFilesDir(DIRECTORY_PICTURES).getAbsolutePath()
, filename);
Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send
email..."));
// Intent intent = new Intent(FlareActivity.this,
AddFlareActivity.class);
//startActivityForResult(intent, ADD_FLARE_RESULT_CODE);
}
});
And here is the piece of code that the error is pointing to (the last line) :
Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);
I have tried it with the provider path being set to both external-files-path and external-path and it doesn't affect the issue
EDIT3: Full stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.fibrnah, PID: 22052
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.android.fibrnah/files/Pictures/FlareData.png
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.android.fibrnah.FlareActivity$2.onClick(FlareActivity.java:84)
at android.view.View.performClick(View.java:6274)
at android.view.View$PerformClick.run(View.java:24859)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Change external-files-path to external-path.
Or, put your files in getExternalFilesDir() (called on a Context). This is the better solution, as your current code will not work on Android Q (by default) and Android R+ (for all apps).

android app download apk file and open it (to install)

I wonder if you can help, I'm trying to download an apk file as an update after version check.
it's downloading the apk but it will not open the apk file its printing an error when trying to open the apk
error :
E/UpdateAPP: Update error! file:///storage/emulated/0/download/update.apk exposed beyond app through Intent.getData()
code :
public class UpdateApp extends AsyncTask<String,Void,Void> {
private Context context;
public void setContext(Context contextf){
context = contextf;
}
#Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "update.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "/update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}}
my targetSdkVersion :
targetSdkVersion 27
thanks for the help
Since API 26, 'REQUEST_INSTALL_PACKAGES' permission is necessary permission to achieve install apk file.
Make 'file_paths.xml' in res/xml folder to use FileProvider api
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="" path="/" />
</paths>
Declare FileProvider and permission in AndroidManifest.xml
Note. I used Androidx version of FileProvider, if you don't use androidx, make sure android.support.v4.content.FileProvider
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="{PACKAGE_NAME}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Get 'Uri' by FileProvider api and request install permission
private fun openFile(file: File) {
val uri = if (Build.VERSION.SDK_INT >= 24) {
val authority = packageName + ".fileprovider"
FileProvider.getUriForFile(this, authority, file)
} else {
Uri.fromFile(file)
}
val myMime = MimeTypeMap.getSingleton()
val mimeType = myMime.getMimeTypeFromExtension(file.extension)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, mimeType)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
if (Build.VERSION.SDK_INT >= 26 && !packageManager.canRequestPackageInstalls()) {
startActivity(
Intent(
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
Uri.parse("package:$packageName")
)
)
} else {
intent.action = Intent.ACTION_VIEW
startActivity(intent)
}
}
Edit -> remove specific methods using in my project.

FileProvider not working

What I am trying to do is to get a file that is in the private area to be opened by another program. In this example, a image viewer app.
AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.myapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/path" />
</provider>
/res/xml/path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="cache" path="cache/"/>
</paths>
Java code (file location)
File file = new File(context.getApplicationInfo().dataDir + File.separator + "cache" + File.separator + filename);
Java code (open file)
if(file != null && file.isFile() && file.canRead() && file.length() > 0){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Uri uri = Uri.parse(file.toURI().toString());
for (int i = 0; i < resInfoList.size(); i++) {
try{
ResolveInfo resolveInfo = resInfoList.get(i);
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}catch(Exception e){
}catch(Error e){
}
}
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(".jpg");
newIntent.setDataAndType(Uri.fromFile(file), mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(newIntent);
return true;
} catch (Exception e) {
}
}
This always results in a toast message "Unable to find item". What am I doing wrong and how to resolve this?
What am I doing wrong
File file = new File(context.getApplicationInfo().dataDir + File.separator + "cache" + File.separator + filename);
Use getCacheDir() to get the cache directory, as your approach is not going to be reliable for secondary accounts on Android 4.2+ tablets and Android 5.0+ phones. So, this should be:
File file = new File(context.getCacheDir(), filename);
Then, you need to replace Uri.fromFile(file) with a call to getUriForFile() on FileProvider, as is covered in the documentation. What you presently have is a Uri pointing to a file, not pointing to a ContentProvider, let alone your FileProvider implementation.
Finally, you will need to call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) on the Intent before you use it with startActivity(), as otherwise the app that responds to the startActivity() request will not have access to the content.
This sample app demonstrates a working implementation of a FileProvider.

Android sharing Files, by sending them via email or other apps

I have a list of files in my android app and I want to be able to get the selected items and send them via email or any other sharing app. Here is my code.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds());
sendIntent.setType("text/plain");
startActivity(sendIntent);
this is the code for sharing file in android
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(myFilePath);
if(fileWithinMyDir.exists()) {
intentShareFile.setType("application/pdf");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myFilePath));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
"Sharing File...");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
Below is another method to share PDF file if you have put file provider in manifest
Uri pdfUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
pdfUri = FileProvider.getUriForFile(PDFViewerActivity.this, fileprovider, 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 file"));
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportPath));
also you can make zip file of all file and attach zip file for send multiple file in android
For those who trying in Kotlin here is the way:
Start the intent like below:
fun startFileShareIntent(filePath: String) { // pass the file path where the actual file is located.
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = FILE_TYPE // "*/*" will accepts all types of files, if you want specific then change it on your need.
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
putExtra(
Intent.EXTRA_SUBJECT,
"Sharing file from the AppName"
)
putExtra(
Intent.EXTRA_TEXT,
"Sharing file from the AppName with some description"
)
val fileURI = FileProvider.getUriForFile(
context!!, context!!.packageName + ".provider",
File(filePath)
)
putExtra(Intent.EXTRA_STREAM, fileURI)
}
startActivity(shareIntent)
}
In Manifest inside the application tag:
<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>
Under res-->xml--> provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="files" path="." />
<external-path name="external_files" path="."/>
</paths>
This is work for every single file!
private void shareFile(File file) {
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType(URLConnection.guessContentTypeFromName(file.getName()));
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://"+file.getAbsolutePath()));
//if you need
//intentShareFile.putExtra(Intent.EXTRA_SUBJECT,"Sharing File Subject);
//intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File Description");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
Thanks Tushar-Mate!
First you should define File Provider, see https://medium.com/#ali.dev/open-a-file-in-another-app-with-android-fileprovider-for-android-7-42c9abb198c1.
The code checks that a device contains applications which can receive the file, see How to check if an intent can be handled from some activity?.
fun sharePdf(file: File, context: Context) {
val uri = getUriFromFile(file, context)
if (uri != null) {
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "application/pdf" // For PDF files.
putExtra(Intent.EXTRA_STREAM, uri)
putExtra(Intent.EXTRA_SUBJECT, file.name)
putExtra(Intent.EXTRA_TEXT, file.name)
// Grant temporary read permission to the content URI.
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
// Validate that the device can open your File.
val activityInfo = intent.resolveActivityInfo(context.packageManager, intent.flags)
if (activityInfo?.exported == true) {
context.startActivity(Intent.createChooser(intent,
"Share PDF file")
}
}
}
fun getUriFromFile(file: File, context: Context): Uri? =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Uri.fromFile(file)
} else {
try {
FileProvider.getUriForFile(context, context.packageName + ".provider", file)
} catch (e: Exception) {
throw if (e.message?.contains("ProviderInfo.loadXmlMetaData") == true) {
Error("FileProvider is not set or doesn't have needed permissions")
} else {
e
}
}
}
Use ACTION_SEND_MULTIPLE for delivering multiple data to someone
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
intent.setType("text/plain");
startActivity(intent);
The arrayUri is the Array List of Uri of files to Send.
Here is an example to share or save a text file:
private void shareFile(String filePath) {
File f = new File(filePath);
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(filePath);
if (fileWithinMyDir.exists()) {
intentShareFile.setType("text/*");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "MyApp File Share: " + f.getName());
intentShareFile.putExtra(Intent.EXTRA_TEXT, "MyApp File Share: " + f.getName());
this.startActivity(Intent.createChooser(intentShareFile, f.getName()));
}
}
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + BuildConfig.APPLICATION_ID + File.separator + DIRECTORY_VIDEO);
String fileName = mediaModel.getContentPath().substring(mediaModel.getContentPath().lastIndexOf('/') + 1, mediaModel.getContentPath().length());
File fileWithinMyDir = new File(directory, fileName);
if (fileWithinMyDir.exists()) {
Uri fileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", fileWithinMyDir);
Intent intent = ShareCompat.IntentBuilder.from(this)
.setStream(fileUri) // uri from FileProvider
.setType("text/html")
.getIntent()
.setAction(Intent.ACTION_SEND) //Change if needed
.setDataAndType(fileUri, "video/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
val uriArrayList: ArrayList<Uri> = ArrayList()
GlobalScope.launch(Dispatchers.IO) {
runCatching {
itemsList!!.forEach {
uriArrayList.add(
FileProvider.getUriForFile(
mContext,
APPLICATION_ID + ".provider",
File(it.path)
)
)
}
}.onSuccess {
requireActivity().runOnUiThread {
if (uriArrayList.size > 0) {
val intent = Intent()
intent.action = Intent.ACTION_SEND_MULTIPLE
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriArrayList)
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.type = "image/*|application/pdf/*"
startActivity(Intent.createChooser(intent, resources.getString(R.string.share)))
}
}
}
.onFailure {
Log.e("SHARING_FAILED", it)
}
}
First of all you have to write provider code in app manifest file for sharing on android 7.0 and above
<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>
here provider_paths are:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="/storage/emulated/0" path="."/>
<root-path name="root" path="." />
<files-path name="files" path="."/>
</paths>
Read this article about Sending Content to Other Apps
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

Categories