Saturday, 24 November 2018
Thursday, 24 May 2018
File sharing from external storage in android using FileProvider
Single/Multiple image/* files from External storage
sharing using FileProvider
Step 1:
Create an file_path provider in res>xml file as
Create an file_path provider in res>xml file as
file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="/"/>
</paths>
Step 2:
Define the File provider in manifest.xml file as
<provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.whatsapp.statusdownloader.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Step 3:
Implement the Button action. For single file share
try {
//storage/emulated/0/Pictures/RAMP/IMG_19May2018_120740.jpg
String path = imagePaths.get(viewPager.getCurrentItem());
Uri contentUri = FileProvider.getUriForFile(ImageOpeningActivity.this, "com.whatsapp.statusdownloader.fileprovider", new File(path))
String shareable = "Status and File Downloader for whatsapp";
Log.e("path uri", newFile.getPath());
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setType(getContentResolver().getType(contentUri)); shareIntent.putExtra(Intent.EXTRA_TEXT, shareable);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
} catch (Exception e) {
e.printStackTrace();
}
For Multiple image share We should use the putParcelableArrayListExtra() for stream.
try {
final SparseBooleanArray selected = mAdapter
.getSelectedIds();
ArrayList<String> shareFiles = new ArrayList<>();
for (int i = 0; i < selected.size(); i++) {
Log.e("selected paths " + i, imageList.get(selected.keyAt(i)).getThumbnail());
shareFiles.add(imageList.get(selected.keyAt(i)).getThumbnail());
}
ArrayList<Uri> filesUri = new ArrayList<>();
for (int i = 0; i < selected.size(); i++) {
filesUri.add(FileProvider.getUriForFile(context,
"com.whatsapp.statusdownloader.fileprovider", new File(shareFiles.get(i))));
}
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setType(context.getContentResolver().getType(filesUri.get(0)));//DataAnd contentUri,
shareIntent.putExtra(Intent.EXTRA_TEXT, "Using statuses and file downloader for whatsapp");
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, filesUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
} catch (Exception e){
e.printStackTrace();
}
Tuesday, 17 April 2018
Subscribe to:
Posts (Atom)