I use FileProvider to share my app's images. While it works perfectly for file sharing via gmail, hangouts, drive etc, I can not share file via non-Google apps ie WhatsApp, Viber.
FileProvider in Androidmanifest
<provider
android:name="android.support.v4.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/filepaths" />
</provider>
filepath.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path path="" name="export" />
</paths>
Method for sharing file
/**
* Static method to export charts data
*
* #param mContext object context
* #param view chart to be converted into image
* #throws IOException
**/
public static void exportChartAsPng(Context mContext, View view) throws IOException {
File baseDir = mContext.getFilesDir();
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String fileName = "chart-" + df.format(calendar.getTimeInMillis()) + ".png";
String filePath = baseDir.toString() + File.separator + fileName;
File chartFile = new File(filePath);
OutputStream outStream = new FileOutputStream(chartFile);
getBitmapFromView(view).compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
try {
Uri fileUri = FileProvider.getUriForFile(
mContext,
"<package.name>.fileprovider",
chartFile);
Intent sendIntent = new Intent("<package.name>.ACTION_RETURN_FILE");
if (fileUri != null) {
// Grant temporary read permission to the content URI
sendIntent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
sendIntent.setType("image/png");
mContext.startActivity(Intent.createChooser(sendIntent, mContext.getString(R.string.share)));
} catch (IllegalArgumentException e) {
Log.e("File Selector",
"The selected file can't be shared: " +
chartFile.getName());
e.printStackTrace();
}
}
Related
I' m trying to create a temporary file and share it.
So I created this class:
public class GenerateFile {
public static File writeToFile(Context mcoContext, String sBody) {
String fileName = "LOG FILE_" + String.valueOf(System.currentTimeMillis()) +".txt";
File file = new File(mcoContext.getCacheDir(), fileName);
try{
FileWriter writer = new FileWriter(file);
writer.append(sBody);
writer.flush();
writer.close();
return file;
}catch (Exception e){
Toast.makeText(mcoContext, "File write failed: " + e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
}
to generate a file that after I will share here:
String logContent = "123";
File filePath = new File(file.getAbsolutePath(), "external_files");
filePath.mkdir();
Uri uri = FileProvider.getUriForFile(StatusActivity.this, getPackageName(), filePath);
Intent intent = ShareCompat.IntentBuilder.from(StatusActivity.this)
.setStream(uri) // uri from FileProvider
.setType("text/html")
.getIntent()
.setAction(Intent.ACTION_VIEW) //Change if needed
.setDataAndType(uri, "text/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
And in the manifest there are already this permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
and the provider declaration
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="android.getqardio.com.gmslocationtest"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
The provider_paths class is defined in this way:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="share"
path="external_files"/>
</paths>
But it generate the message, when I try to share it by mail or telegram "Unable to attach file" or "Unsupported attachment". Also it seems to me that the file is not created.
Other apps do not have access to your app's getCacheDir(). FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION are for content Uri values, not file Uri values. And, on Android 7.0+ devices, your code should crash with a FileUriExposedException.
Use FileProvider to make your content available to other apps, and use FileProvider.getUriForFile() to get the Uri to put in the Intent.
So I follow the suggestion of #CommonsWare, and I edited my code. This is the final result:
public class GenerateFile {
public static Uri getFileURI(Context context, String nameFile, String content, String fileExtension) {
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
Date date = new Date();
String fileName = dateFormat.format(date)+nameFile+fileExtension;
File file = new File(context.getCacheDir(), fileName);
try{
FileWriter writer = new FileWriter(file);
writer.append(content);
writer.flush();
writer.close();
//Toast.makeText(context, "Writing to the file completed successfully", Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(context, "File writing failed: " + e.toString(), Toast.LENGTH_LONG).show();
}
File filePath = new File(context.getCacheDir(), "");
File newFile = new File(filePath, fileName);
return FileProvider.getUriForFile(context, "MYPACKAGE.fileprovider", newFile);
}
}
and in another class:
private void sendFile(String nameFile, String logContent, String fileExtension) {
Uri contentUri = GenerateFile.getFileURI(getApplicationContext(), nameFile, logContent, fileExtension);
Intent intent = ShareCompat.IntentBuilder.from(StatusActivity.this)
.setStream(contentUri) // uri from FileProvider
.setType("text/plain")
.getIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "send"));
}
so to send the file. I also deleted the permission (previously mentioned) in the manifest, because I didn't need it anymore.
And I also edited my provider and provider_path file like that:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="MYPACKAGE.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<cache-path
name="my_files"
path=""
/>
Now it works! Thank you very much guys for the help!
Did you specifically ask the user for those permissions? It's not enough to just put the permissions in the manifest for target sdks below 28. Also, in Android Q, you will need to work around external storage permissions altogether as this is disallowed.
Edit
My functions now look like this:
p
If uou want to save image in your external storage use Externalstoragepublicdirectory function.
This intent expects the EXTRA_OUTPUT location in Uri format, see https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE. Some devices may understand an absolute file path, but this is not the documented behavior.
You can use getExternalMediaDirs() to avoid many permission restrictions.
I have made some changes to your code:
Add this to you application level in your Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Then create file_paths.xml file:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="my_cache" path="." />
<external-path name="my_images" path="Pictures/" />
</paths>
Then in your code:
private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";
private String mTempPhotoPath;
public void takePhoto() {
// Create the capture image intent
Intent imageCapture= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (imageCapture.resolveActivity(getPackageManager()) != null) {
// Create the temporary File where the photo should go
File photoFile = null;
try {
photoFile = createTempImageFile(this);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
// Get the path of the temporary file
mTempPhotoPath = photoFile.getAbsolutePath();
// Get the content URI for the image file
Uri imageUri = FileProvider.getUriForFile(this,
FILE_PROVIDER_AUTHORITY,
photoFile);
// Add the URI so the camera can store the image
imageCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
}
static File createTempImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalCacheDir();
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
}
//To save image
String mCurrentPhotoPath = null;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/DCIM/Camera");
Log.d(TAG, "createImageFile: Saving image to:" + storageDir);
File image = new File(storageDir, imageFileName);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
//Return the saved image
return mCurrentPhotoPath ;
}
I have an app in which I capture image and set it to the ImageView and then upload it to the server. But whenever I capture images the image is not getting displayed and when I try to upload the image I get FileNotFoundException as the path does not contain the image.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.imageuploader.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Image Uploader" path="Android/data/com.example.android.imageuploader/files/Pictures" />
</paths>
When i create image file I use this,
private File createImageFile() {
String timeStamp = new SimpleDateFormat(
getString(R.string.time_stamp_format), Locale.getDefault())
.format(new Date());
String fileName = getString(R.string.file_name_format, timeStamp);
File storageDirectory =
new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
if (!(storageDirectory.exists() || storageDirectory.mkdirs())) {
Helper.showToastMessage(mContext, getString(R.string.warn_storage_dir));
}
return new File(storageDirectory.getPath() + File.separator + fileName);
}
Actually I am saving the image in the internal storage directory 'Pictures' and in it I am creating folder after the app's name in which all the images are being saved. But while setting the image to the ImageView I am getting different file path say,
Image Uploader/Pictures/Image Uploader/20180406_101234.jpg
which is why the image is not being displayed as well as uploaded.
Where I am going wrong I am not able to figure that out. Please help.
This is how am doing, this works perfectly.
AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yourpackagename.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_path"/>
</provider>
provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="/storage/emulated/0" path="."/>
</paths>
createImageFile() make sure you have read and write external storage permission
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "MYAPPNAME-" + timeStamp + ".png";
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
"YourAppFolder");
File storageDir = new File(mediaStorageDir + "/Profile_Images");
if (!storageDir.exists())
{
storageDir.mkdirs();
}
File image = new File(storageDir, imageFileName);
return image;
}
click listener on button to take camera image
===Global Variables===
Uri mUri;
private static final int CAMERA_IMAGE_RESULT = 202;
private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackagename.fileprovider";
===Global Variables===
takeImageBTN.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
File file = null;
try
{
file = createImageFile();
mUri = FileProvider.getUriForFile(this,
CAPTURE_IMAGE_FILE_PROVIDER, file);
Log.d("uri", mUri.toString());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(cameraIntent, CAMERA_IMAGE_RESULT);
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
Then lastly onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case CAMERA_IMAGE_RESULT:
{
if (resultCode == RESULT_OK)
{
if (mUri != null)
{
Log.d("uriPath", mUri.getPath().replace("//", "/"));
String profileImageFilepath = mUri.getPath().replace("//", "/");
Log.d("path", profileImageFilepath);
profileIV.setImageURI(mUri);
/*Your Asynctask to upload Image using profileImageFilepath*/
new PostDataAsyncTask().execute(profileImageFilepath);
}
}
break;
}
}
}
take run time permission for
<!-- == External Storage == -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am in trouble with this issue, couldn't find any way out yet.
I have configured a Fileprovider in manifest.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.package.name.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
where the #xml/file_paths is like
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="my_images"
path="Android/data/com.package.name/files/Pictures" />
<external-files-path
name="my_images_" />
</paths>
And my java code is like below
private void selectImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("selectImageFromCamera", "Error: " + ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
try {
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.package.name.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_PIC_REQUEST);
} catch (Exception e) {
e.printStackTrace();
Log.e("selectImageFromCamera", "Error: " + e);
}
}
}
The method createImageFile()
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Log.e("storageDir", "storageDir: " + storageDir.getAbsolutePath());
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
myCurrentPhotoPath = image.getAbsolutePath();
return image;
}
But I am always getting problem when creating photoUri, to be exactly at below line.
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.package.name.fileprovider",
photoFile);
Error in try catch
.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.package.name/files/Pictures/JPEG_20170417_115925_834197983.jpg
Any help would be appreciated. Thanks.
I am going to take a photo from Camera in Xamarin Android.
Here's what I did in Xamarin.
Start Camera Activity
private void CallTakePictureIntent ()
{
Intent takePictureIntent = new Intent (MediaStore.ActionImageCapture);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.ResolveActivity (PackageManager) != null) {
// Create the File where the photo should go
Java.IO.File photoFile = null;
try {
photoFile = ImageHelper.CreateImageFile (this);
} catch (Exception ex) {
System.Console.WriteLine (ex.ToString ());
}
// Continue only if the File was successfully created
if (photoFile != null) {
sURI photoURI = FileProvider.GetUriForFile (this, UIHelper.FileProvider, photoFile);
requestedAvatarUri = photoURI;
takePictureIntent.PutExtra (MediaStore.ExtraOutput, photoURI);
StartActivityForResult (takePictureIntent, REQUEST_TAKE_PICTURE);
}
}
}
Create temporary file.
public static Java.IO.File CreateImageFile (Context context)
{
// Create an image file name
string timeStamp = DateTime.Now.ToString ("yyyyMMdd_HHmmss");
string imageFileName = "JPEG_" + timeStamp + "_";
Java.IO.File storageDir = context.GetExternalFilesDir (Android.OS.Environment.DirectoryPictures);
Java.IO.File image = Java.IO.File.CreateTempFile (
imageFileName, /* p refix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
Android.Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.masterbee.xamapp-dev.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
xml/file_paths.xml
<external-path name="external_images" path="Pictures/" />
I think I've done correctly, all things work except runtime exception in FileProvider.GetUriForFile() line.
Here's call trace.
enter image description here