Unable to sharing image file with other app using FileProvider - android

I have image list and I want to share image that selected.To do, I have used FileProvider. I'm running app on Android 10. Also I have tried without shareIntent(Intent.EXTRA_STREAM,uri), then app opening only share via dialogs.After not sharing anything just returning the app.
in 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/file_provider_paths" />
</provider>
in paths;
<paths>
<files-path
name="files"
path="/" />
<cache-path
name="cache"
path="/" />
here is the how I used and start intent,
int pos = getAdapterPosition();
File imageFile = new File(imageQuotes.get(pos).getPath());
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", imageFile);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setDataAndType(uri,"image/*");
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
mContext.startActivity(shareIntent);
When run the app,after I have choosed WhatsApp for ie. My image file sent as a BIN file and not has image preview on whatsapp.
uri is not null and is content://com.scoto.partestestapplication.provider/files/SavedImageQuotes/Partes_1601815401590

Related

How do I programmatically open an external folder in Android?

My app downloads an epub file to /external/downloads/{MyFolder}/. I can normally auto-open the file in an e-reader like Play Books. As a fall back, however, in case of no installed e-reader, I want to open the device's default File Browser to MyFolder. On my device, that browser program is called "My Files".
I'm getting an ActivityNotFoundException. How do I programmatically open that File Browser? Ideally to the {MyFolder} location.
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
File file = new File(fileUri.getPath());
File folder = new File(file.getParent());
Uri folderUri = Uri.fromFile(folder);
intent.setDataAndType(folderUri, "resource/folder");
getContext().startActivity(intent);
} catch (Exception e1) {
Toast.makeText(getContext(), "Ebook saving to " + fileUri.getPath(), Toast.LENGTH_LONG).show();
}
For the record, fileUri.toString() for the epub file is "content://media/external/downloads/16143".
.startActivity(intent) throws the exception:
No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.{mydomain}.app.provider/root/external/downloads typ=resource/folder flg=0x1 }
I've also attempted to change folderUri declaration to the following, and get similar errors.
final ContentValues v = new ContentValues();
v.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/{MyFolder}/");
final ContentResolver r = getContext().getContentResolver();
Uri folderUri = r.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, v);
My AndroidManifest.xml has
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.{mydomain}.app.provider"
android:exported="false"
android:grantUriPermissions="true">
<!-- resource file to create -->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
And my file_paths.xml has
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<root-path name="root" path="." />
</paths>
I've also attempted with the paths being "./", and with no root-path element, and it makes no difference.
If you want directly browse the Camera folder in public DCIM folder with the Files app:
String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(scheme), "vnd.android.document/root");
startActivity(intent );
I assume you can easily change that for Download/MyFolder.
I think this only works for Android 11+ devices. Please try out.

How to share Videos to any app using Intent?

Issue: What happens is after sharing the video when i launch the app to check the shared video it shows me error of "Unable to import" and "The video is no longer available".
Android Version: Oreo(8.0)
Code: I am trying with the following code to share videos with other apps using intent:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("video/*");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
File imageFileToShare = new File(Environment.getExternalStorageDirectory() + "/mygallery/" + "airp.mp4");
Uri uri = FileProvider.getUriForFile(Main2Activity.this, "abc.dcf.fileprovider", imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.abc.in");
startActivity(Intent.createChooser(share, "Message"));
The above code is working fine to share Images but its not working to share videos.
What i have tried: I have tried by sharing various file types like 3gp, mkv, mp4 but nothing is working out and I have also went through various similar questions but no solution is working in case of videos.
Please help me with this issue and also if there is any alternate way to do so?
You can add this to your OnCreate
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
If that doesn't work for you,
Add Provider to Manifest by doing this
<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>
Remember to use android:name="androidx.core.content.FileProvider" if you're using androidx
Create a file in xml directory (res/xml) and name provider_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>
Replace
Uri uri = FileProvider.getUriForFile(Main2Activity.this, "abc.dcf.fileprovider", imageFileToShare);
with this
Uri uri = FileProvider.getUriForFile(Main2Activity.this, BuildConfig.APPLICATION_ID + ".provider",imageFileToShare);

How to open files (from SD-Card) in gallery

I save paths of different jpg. files in a database. These images are partly in the internal memory and partly in the external memory (SD-card).
Now I want to open the pictures in the gallery.
To open images from the internal memory I use the FileProvider, I proceed as follows:
String path = "/storage/emulated/0/Pictures/Meetings/IMG_Testmeeting20180809_225414.jpg";
File image = new File(path);
Uri uri = FileProvider.getUriForFile(getContext(), "com.meetings.martin.provider",image);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
getContext().startActivity(intent);
This is my FileProvider in the AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.meetings.martin.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths">
</meta-data>
</provider>
This is content of the file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
</paths>
This works perfectly..
..but , if I want to open an image, which is located on the SD card, I get the following error message:
Failed to find configured root that contains <path>
I've often read that the FileProvider can not access files on the SD card. But how can I then turn the path into a Uri and open it in the gallery?
I solved it.
I added following line to the path file:
<root-path name="external_files" path="/storage/" />

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.

Categories

Resources