My app is providing the functionality to share files between users. For image files there is a custom gallery, and by long clicking, it opens images in local Image Viewer via ACTION_VIEW intent. Here is the code:
public static boolean openFile(Context context, File file)
{
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String extension = FilenameUtils.getExtension(file.getPath());
String mimeType = mimeTypeMap.getMimeTypeFromExtension(extension.toLowerCase());
Intent openFile = new Intent(Intent.ACTION_VIEW);
openFile.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|Intent.FLAG_ACTIVITY_NO_HISTORY
|Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri fileUri;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
String authority = context.getApplicationContext().getPackageName() + ".provider";
try {
fileUri = FileProvider.getUriForFile(context, authority, file);
} catch (IllegalArgumentException e) {
if (Fabric.isInitialized()) Crashlytics.logException(e);
else e.printStackTrace();
return false;
}
}
else fileUri = Uri.fromFile(file);
openFile.setDataAndType(fileUri, mimeType != null ? mimeType : "*/*");
if (openFile.resolveActivity(context.getPackageManager()) != null)
{
context.startActivity(openFile);
return true;
}
return false;
}
All works fine on API < 24, but on 24+ I've faced an issue trying to open image on sdcard. The exception is:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/89F3-1B06/Pictures/DSC_0004.JPG
W/System.err: at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
W/System.err: at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
Apparently, this is due to the fact that <external-path name="other" path="."/> doesn't provide access on the level of sdcard in the file tree. So I've added this path:
<external-path name="external_files" path="../../"/> . That helped, but something tells me, that it's not the best solution.
I've found another solution, which works too:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
fileUri = Uri.fromFile(file);
But disabling StrictMode restrictions looks even worse. So it's not on option.
Any suggestion on how to solve this issue the right way?
FileProvider cannot serve files from removable SD card.
Only from getFilesDir(), getExternalFilesDir() and getExternalStorageDirectory().
You should make your own file provider if you wanna serve files from sd card.
Related
My app has to copy a file from private app folder to a SAF folder with the suitable permissions granted.
The used code is:
static boolean copyFileToTargetFolderWithNewName(Activity activity, String docUri,String targetFolderUri,String newName)
{
deleteIfExisting(activity,Uri.parse(targetFolderUri),newName);
File newFile = new File(docUri);
Uri contentUri = FileProvider.getUriForFile(activity, "com.myappname.fileprovider", newFile);
ContentResolver resolver = activity.getContentResolver();
boolean result=false;
Log.d("copy",contentUri+" "+targetFolderUri+" "+newName);
try {
//error here
Uri newUri=DocumentsContract.copyDocument(resolver,contentUri,Uri.parse(targetFolderUri));
DocumentsContract.renameDocument(resolver,newUri,newName);
result=true;
} catch (FileNotFoundException e) {
result=false;
}
return result;
}
The log output yields:
D/copy: content://com.myappname.fileprovider/external_files/Android/data/com.myappname.app/files/subfolder/file.txt
content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FSAFfolder/subfolder
newname.txt
What's wrong?
Is this a flags problem? Are the two domains not compatible for copying?
You are trying to copy a non-document Uri (contentUri), and the JavaDocs for copyDocument() state that the first parameter needs to be a document Uri:
document with Document#FLAG_SUPPORTS_COPY This value must never be null.
When DocumentsContract refers to a Uri as a "document", it means a Uri obtained through DocumentsContract itself. That would include a Uri obtained through ACTION_OPEN_DOCUMENT or a Uri obtained through a tree DocumentFile. However, that will not cover:
A Uri created by Uri.fromFile()
A Uri from FileProvider.getUriForFile()
A Uri created by Uri.parse() from an http or https URL
Etc.
I'm using FileProvider API for sharing content actually storing in internal storage.
Following is my xml configuration that linked with Provider configured in Manifiest file.
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="xs_audio" path="/audio/records"/>
</paths>
and code that I'm using to share is following:
private final static String FILE_PROVIDER = BuildConfig.APPLICATION_ID + ".fileprovider";
private String testPackageAppHaveAccess = "com.whats";
public static void shareDocument(Activity activity, CallRecordData data) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Record File");
intent.setType("audio/*");
ArrayList<Uri> files = new ArrayList<>();
//for (AudioModelObj image : data.getDocuments()) {
files.add(getImageUri(activity, data.getFile()));
//}
activity.grantUriPermission(testPackageAppHaveAccess, files.get(0), Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
activity.startActivity(Intent.createChooser(intent, "Share Audio File."));
}
private static Uri getImageUri(Activity activity, String audioURI) {
if (Build.VERSION.SDK_INT < 24) {
return Uri.parse(audioURI);
} else {
URI uri = URI.create(audioURI);
File file = new File(uri);
return FileProvider.getUriForFile(activity, FILE_PROVIDER, file);
}
}
}
but while launching with app it's not attaching anything. In case of gmail it say "can't attach empty file". File is confirmedly available as I'm displaying list of file and playing.
For reference: Uri generating from getImageUri(..) is
/data/user/0/com.xl.cl.debug/cache/audio/records/17-10-17_170728_abc_.wav
Any suggestion what I'm doing wrong ?
<files-path> already points to what on some devices will be /data/user/0/com.xl.cl.debug
<cache-path> is what you should be using, replacing path with just the subdirectory of interest (audio/records), eliminating the /data/user/0/com.xl.cl.debug/cache bit
You are calling grantUriPermission(), where the first parameter is not a package
You are calling grantUriPermission(), where the first parameter is not a package identifying the app to which you are trying to grant permission
You are not adding FLAG_GRANT_READ_URI_PERMISSION to the Intent, which is the typical way of saying "the Uri in this Intent should be readable by the recipient of this Intent" (though it is possible that ACTION_SEND_MULTIPLE requires more work here, as I haven't played with that much)
No filesystem path in the human history has begun with content:/, so get rid of that
Calling new File() and supplying a value that is not a filesystem path is not going to work well
There may be more problems than those, but that should get you started
I am trying to share image file in cache directory, i have the complete path, but not able to send the file in attachments, the code is
File shareImage=Utils.getBitmapFile();
Log.d("Activity", "get final path in result"+shareImage.getAbsolutePath());
/*MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=shareImage.getName().substring(shareImage.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
shareIntent.setType(type);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri shareImageUri = Uri.fromFile(shareImage);
Uri shareImageUri = Uri.fromParts("content", shareImage.getAbsolutePath(), null);//("content://"+shareImage.getAbsolutePath());
*/
Uri shareImageUri = Uri.fromFile(shareImage);
Log.d("Result ","uri is "+shareImageUri.toString());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Results"));
the above commented code is not working
the send mail shows attachment,but not receiving end there is not attachment,
facebook sharing also shows no image in post
what the reason for this??
I have already seen the following SO Links how-to-use-share-image-using-sharing-intent-to-share-images-in-android and many others, none of them are able to resolve the issue
P.S.;
1.The aim is to take screenshot of screen save it in cache directory and share it online from there
2.Yes i do have file, I can pull it via DDMS from device and see on system.
I followed #CommonsWare's advice and used a FileProvider. Assuming your image is already in the internal cache directory as cache/images/image.png, then you can use the following steps. These are mostly a consolidation of the documentation.
Set up the FileProvider in the Manifest
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
...
</application>
</manifest>
Replace com.example.myapp with your app package name.
Create res/xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="shared_images" path="images/"/>
</paths>
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Share the image
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
Documentation
FileProvider
Storage Options - Internal Storage
Sharing Files
Saving Files
what the reason for this?
As noted, other apps do not have access to your app's internal storage.
none of them are able to resolve the issue
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what specific solutions you have tried and what specific problems you have encountered.
but that does not seems to be working as per SO post!!!
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what "that does not seems to be working" means.
Or, use FileProvider, which offers this capability with no code required beyond an entry for it in your manifest.
Or, store your image on external storage, such as getExternalCacheDir().
I share cached image by followed steps .
1.Copy your cached image to target path.
public static File copyImage(String sourcePath, String targetPath){
try {
InputStream in = new FileInputStream(sourcePath);
OutputStream out = new FileOutputStream(targetPath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return new File(targetPath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
2.Get the Uri of copy file.
Uri uri = Uri.fromFile(target);
3.Share image by intent
File dir = new File(Constant.INKPIC_PATH);//your custom path,such as /mnt/sdcard/Pictures
if(!dir.exists()){
dir.mkdirs();
}
File f = new File(dir,"temporary_file.jpg");
File target = copyImage(url,f.getAbsolutePath());
Uri uri = Uri.fromFile(target);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,uri );
context.startActivity(intent);
I am trying to share image file in cache directory, i have the complete path, but not able to send the file in attachments, the code is
File shareImage=Utils.getBitmapFile();
Log.d("Activity", "get final path in result"+shareImage.getAbsolutePath());
/*MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=shareImage.getName().substring(shareImage.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
shareIntent.setType(type);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri shareImageUri = Uri.fromFile(shareImage);
Uri shareImageUri = Uri.fromParts("content", shareImage.getAbsolutePath(), null);//("content://"+shareImage.getAbsolutePath());
*/
Uri shareImageUri = Uri.fromFile(shareImage);
Log.d("Result ","uri is "+shareImageUri.toString());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Results"));
the above commented code is not working
the send mail shows attachment,but not receiving end there is not attachment,
facebook sharing also shows no image in post
what the reason for this??
I have already seen the following SO Links how-to-use-share-image-using-sharing-intent-to-share-images-in-android and many others, none of them are able to resolve the issue
P.S.;
1.The aim is to take screenshot of screen save it in cache directory and share it online from there
2.Yes i do have file, I can pull it via DDMS from device and see on system.
I followed #CommonsWare's advice and used a FileProvider. Assuming your image is already in the internal cache directory as cache/images/image.png, then you can use the following steps. These are mostly a consolidation of the documentation.
Set up the FileProvider in the Manifest
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
...
</application>
</manifest>
Replace com.example.myapp with your app package name.
Create res/xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="shared_images" path="images/"/>
</paths>
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Share the image
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
Documentation
FileProvider
Storage Options - Internal Storage
Sharing Files
Saving Files
what the reason for this?
As noted, other apps do not have access to your app's internal storage.
none of them are able to resolve the issue
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what specific solutions you have tried and what specific problems you have encountered.
but that does not seems to be working as per SO post!!!
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what "that does not seems to be working" means.
Or, use FileProvider, which offers this capability with no code required beyond an entry for it in your manifest.
Or, store your image on external storage, such as getExternalCacheDir().
I share cached image by followed steps .
1.Copy your cached image to target path.
public static File copyImage(String sourcePath, String targetPath){
try {
InputStream in = new FileInputStream(sourcePath);
OutputStream out = new FileOutputStream(targetPath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return new File(targetPath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
2.Get the Uri of copy file.
Uri uri = Uri.fromFile(target);
3.Share image by intent
File dir = new File(Constant.INKPIC_PATH);//your custom path,such as /mnt/sdcard/Pictures
if(!dir.exists()){
dir.mkdirs();
}
File f = new File(dir,"temporary_file.jpg");
File target = copyImage(url,f.getAbsolutePath());
Uri uri = Uri.fromFile(target);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,uri );
context.startActivity(intent);
I try to export a bitmap from my app using share intent without saving a file for a temporal location. All the examples I found are two-step
1) save to SD Card and create Uri for that file
2) start the intent with this Uri
Is it possible to make it without requiring WRITE_EXTERNAL_STORAGE permission, saving the file [and removing it afterwards]? How to address devices without ExternalStorage?
I had this same problem. I didn't want to have to ask for the read and write external storage permissions. Also, sometimes there are problems when phones don't have SD cards or the cards get unmounted.
The following method uses a ContentProvider called FileProvider. Technically, you are still saving the bitmap (in internal storage) prior to sharing, but you don't need to request any permissions. Also, every time you share the bitmap the image file gets overwritten. And since it is in the internal cache, it will be deleted when the user uninstalls the app. So in my opinion, it is just as good as not saving the image. This method is also more secure than saving it to external storage.
The documentation is pretty good (see the Further Reading below), but some parts are a little tricky. Here is a summary that worked for me.
Set up the FileProvider in the Manifest
<manifest>
...
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
...
</application>
</manifest>
Replace com.example.myapp with your app package name.
Create res/xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="shared_images" path="images/"/>
</paths>
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Save the image to internal storage
// save bitmap to cache directory
try {
File cachePath = new File(context.getCacheDir(), "images");
cachePath.mkdirs(); // don't forget to make the directory
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
Share the image
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.myapp.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
Further reading
FileProvider
Storage Options - Internal Storage
Sharing Files
Saving Files
I try to export a bitmap from my app using share intent without saving a file for a temporal location.
In theory, this is possible. In practice, it is probably not possible.
In theory, all you need to share is a Uri that will resolve to the bitmap. The simplest approach is if that is a file that is directly accessible by the other application, such as on external storage.
To not write it to flash at all, you would need to implement your own ContentProvider, figure out how to implement openFile() to return your in-memory bitmap, and then pass a Uri representing that bitmap in the ACTION_SEND Intent. Since openFile() needs to return a ParcelFileDescriptor, I don't know how you would do that without an on-disk representation, but I have not spent much time searching.
Is it possible to make it without requiring WRITE_EXTERNAL_STORAGE permission, saving the file [and removing it afterwards]?
If you simply do not want it on external storage, you can go the ContentProvider route, using a file on internal storage. This sample project demonstrates a ContentProvider that serves up a PDF file via ACTION_VIEW to a PDF viewer on a device; the same approach could be used for ACTION_SEND.
If anyone still looking for easy and short solution without any storage permission (Supports nougat 7.0 as well). Here it is.
Add this in Manifest
<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>
Now create provider_paths.xml
<paths>
<external-path name="external_files" path="."/>
</paths>
Finally Add this method to your activity/fragment (rootView is the view you want share)
private void ShareIt(View rootView){
if (rootView != null && context != null && !context.isFinishing()) {
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
if (bitmap != null ) {
//Save the image inside the APPLICTION folder
File mediaStorageDir = new File(AppContext.getInstance().getExternalCacheDir() + "Image.png");
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(mediaStorageDir));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (ObjectUtils.isNotNull(mediaStorageDir)) {
Uri imageUri = FileProvider.getUriForFile(getActivity(), getActivity().getApplicationContext().getPackageName() + ".provider", mediaStorageDir);
if (ObjectUtils.isNotNull(imageUri)) {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(waIntent, "Share with"));
}
}
}
}
}
Update:
As #Kathir mentioned in comments,
DrawingCache is deprecated from API 28+. Use below code to use Canvas instead.
Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), quality);
Canvas canvas = new Canvas(bitmap);
Drawable backgroundDrawable = view.getBackground();
if (backgroundDrawable != null) {
backgroundDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return bitmap;
This for sharing CardView as an Image then saving it in the cache subdirectory of the app's internal storage area.
hope it will be helpful.
#Override
public void onClick(View view) {
CardView.setDrawingCacheEnabled(true);
CardView.buildDrawingCache();
Bitmap bitmap = CardView.getDrawingCache();
try{
File file = new File(getContext().getCacheDir()+"/Image.png");
bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(file));
Uri uri = FileProvider.getUriForFile(getContext(),"com.mydomain.app", file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
getContext().startActivity(Intent.createChooser(shareIntent, "Share"));
}catch (FileNotFoundException e) {e.printStackTrace();}
}
});
Here is working method to make a screenshot of own app and share it as image via any messanger or email client.
To fix the bitmap not updating problem I improved Suragch's answer, using Gurupad Mamadapur's comment and added own modifications.
Here is code in Kotlin language:
private lateinit var myRootView:View // root view of activity
#SuppressLint("SimpleDateFormat")
private fun shareScreenshot() {
// We need date and time to be added to image name to make it unique every time, otherwise bitmap will not update
val sdf = SimpleDateFormat("yyyyMMdd_HHmmss")
val currentDateandTime = sdf.format(Date())
val imageName = "/image_$currentDateandTime.jpg"
// CREATE
myRootView = window.decorView.rootView
myRootView.isDrawingCacheEnabled = true
myRootView.buildDrawingCache(true) // maybe You dont need this
val bitmap = Bitmap.createBitmap(myRootView.drawingCache)
myRootView.isDrawingCacheEnabled = false
// SAVE
try {
File(this.cacheDir, "images").deleteRecursively() // delete old images
val cachePath = File(this.cacheDir, "images")
cachePath.mkdirs() // don't forget to make the directory
val stream = FileOutputStream("$cachePath$imageName")
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream) // can be png and any quality level
stream.close()
} catch (ex: Exception) {
Toast.makeText(this, ex.javaClass.canonicalName, Toast.LENGTH_LONG).show() // You can replace this with Log.e(...)
}
// SHARE
val imagePath = File(this.cacheDir, "images")
val newFile = File(imagePath, imageName)
val contentUri = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", newFile)
if (contentUri != null) {
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // temp permission for receiving app to read this file
shareIntent.type = "image/jpeg" // just assign type. we don't need to set data, otherwise intent will not work properly
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
startActivity(Intent.createChooser(shareIntent, "Choose app"))
}
}