I am having trouble trying to show an image in a remote view with a contentUri. I have outlined the steps I took below. I believe I have followed the procedure correctly but still get a "Can't load widget" message. What am I doing wrong? Is the image saved to the correct storage area? Is the contentUri constructed correctly? Please help.
Blockquote
Save the image into my file directory. I can see the image successfully saved in data/data/com.mydomain/files/myImage.jpg when I check using Android Studio's Device File Explorer.
val file = File(applicationContext.filesDir, "myImage.jpg")
try {
FileOutputStream(file).use { out ->
myBitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)
}
} catch (e: IOException) {
e.printStackTrace()
}
Create a FileProvider in manifest and provider_paths.xml in res.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/provider_paths" />
</provider>
<paths>
<files-path name="name" path="." />
</paths>
Create a content Uri using getUriForFile. This returns a content uri of: content://com.mydomain.provider/name/myImage.jpg
val file = File(getAppInstance().filesDir, "myImage.jpg")
val contentUri = FileProvider.getUriForFile(
getAppInstance(),
BuildConfig.APPLICATION_ID + ".provider",
file
)
I pass the content Uri into Image() composable.
Image(
modifier = GlanceModifier.size(28.dp),
provider = ImageProvider(contentUri),
contentDescription = "Image"
)
Result on run:
There have been several posts about this that I have reviewed and I've tried a half a dozen of the proposed solutions. I have something almost identical to this working without throwing an exception in another app I wrote but I'm just not able to get this not to throw an exception, although the file does get transferred! The permission is being set and the file does transmit just fine, but an exception still gets thrown although it does not crash the app.
What I have is this:
fun shareVideo(videoFile: File, context: Context) {
if(videoFile.exists()) {
Timber.i("Video file exists and the length in bytes is: ${videoFile.length()}")
} else {
Timber.w("Video file does not exist. Exiting.")
return
}
val uris = arrayListOf<Uri>()
val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", videoFile)
Timber.i("Uri: $uri + path: ${uri.path}")
uris.add(uri)
val intent = Intent()
intent.action = Intent.ACTION_SEND_MULTIPLE
intent.putExtra(Intent.EXTRA_SUBJECT, "Shared files")
intent.type = "video/mp4"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Timber.i("Intent: $intent")
try{
ContextCompat.startActivity(context, Intent.createChooser(intent, "Shared Videos"), null)
} catch (e: Exception) {
Timber.e("Exception starting activity. \nException was ${e.message}\n Stack trace to follow:\n ${e.stackTrace}")
}
}
The stack trace looks like this:
2021-03-07 14:21:59.570 6039-6207/com.company.app E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.company.app.provider/external_files/1615148514218.mp4 from pid=32136, uid=1000 requires the provider be exported, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:820)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:684)
at android.content.ContentProvider$Transport.query(ContentProvider.java:239)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:106)
at android.os.Binder.execTransactInternal(Binder.java:1154)
at android.os.Binder.execTransact(Binder.java:1123)
The Timber log output looks like this:
2021-03-07 14:33:32.713 7396-7396/com.company.app I/StorageUtilsKt: Video file exists and the length in bytes is: 7511455
2021-03-07 14:33:32.719 7396-7396/com.company.app I/StorageUtilsKt: Uri: content://com.company.app.provider/external_files/1615149206171.mp4 + path: /external_files/1615149206171.mp4
2021-03-07 14:33:37.337 7396-7396/com.company.app I/StorageUtilsKt: Intent: Intent { act=android.intent.action.SEND_MULTIPLE typ=video/mp4 flg=0x10000001 (has extras) }
2021-03-07 14:33:38.604 7396-7589/com.company.app E/DatabaseUtils: Writing exception to parcel
So the file is there, and I see it in the attachment that it does add it and I do get it via email/etc. and can view it, but the exception is thrown in the logs.
My provider_paths.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="/"/>
<external-cache-path name="external_cache" path="." />
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<files-path name="app_videos" path="." />
</paths>
And my manifest has this in it:
<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>
Everything looks exactly like the app I have that works yet this one throws that exception.
Can anyone see what's wrong with what I am doing?
Check this one here https://stackoverflow.com/a/59439316
you need to grant permissions to each available app which is available for this intent.
Use ShareCompat.IntentBuilder. It is doing all the right things for you.
For the underlying issue, see https://issuetracker.google.com/issues/173137936
fun shareVideo(videoFile: File, context: Context) {
if (videoFile.exists()) {
Timber.i("Video file exists and the length in bytes is: ${videoFile.length()}")
} else {
Timber.w("Video file does not exist. Exiting.")
return
}
val uri = FileProvider.getUriForFile(context, context.packageName + ".provider", videoFile)
ShareCompat.IntentBuilder(context)
.setType("video/mp4")
.setSubject("Shared files")
.addStream(uri)
.setChooserTitle("Shared Videos")
.startChooser()
}
I have integrated Snapchat's Creative Kit in my Android app. After processing, I receive an image from the server in the form of Byte Array which I am saving to the disk and then sending the file to the Snapchat's Creative Kit as shown below.
private fun downloadImage(
fileName: String,
imageByteArray: ByteArray?): Uri? {
val state = Environment.getExternalStorageState()
if (Environment.MEDIA_MOUNTED == state) {
val downloadDir = File(
Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name)
)
if (!downloadDir.isDirectory) {
downloadDir.mkdirs()
}
val file = File(downloadDir, fileName)
var ostream: FileOutputStream? = null
try {
ostream = FileOutputStream(file)
ostream.write(imageByteArray)
ostream.flush()
ostream.close()
}
} catch (e: IOException) {
e.printStackTrace()
}
val snapCreativeKitApi = SnapCreative.getApi(context!!)
val snapMediaFactory = SnapCreative.getMediaFactory(context!!)
lateinit var snapPhotoFile: SnapPhotoFile
try {
snapPhotoFile = snapMediaFactory.getSnapPhotoFromFile(file)
} catch (e: SnapMediaSizeException) {
return
}
val snapPhotoContent = SnapPhotoContent(snapPhotoFile)
snapCreativeKitApi.send(snapPhotoContent)
}
}
I have also added provider in the manifest file as shown below:
<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_app" />
</provider>
And in the provider_paths_app.xml, I have tried all the possible paths by referring this answer and none of them works.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="My App Name"
path="." />
</paths>
With the above path, I am getting the below error.
Couldn't find meta-data for provider with authority my.package.name.fileprovider
All I have to do is send this image to Snapchat but I am unable to figure out what I am doing wrong. Any help will be appreciated.
First, write the following tag in manifest under 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>
Then create a xml folder in res and create a file named: provider_paths.xml
and then copy paste the code:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
And now here's where most developers make mistakes: in your script, create your File with:
FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", file);
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
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 my provider declaration, the value of ${applicationId} is "com.limxtop.research", make sure that the name of authorities is the same with that of the codes below.
// Create the file where the photo should save.
File file = null;
try {
file = createImageFile();
} catch (IOException e) {
break;
}
// The second parameter is the name of authorities.
Uri uri = FileProvider.getUriForFile(this,
"com.limxtop.research.fileprovider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, fullSizeRequestCode);
So, maybe your codes post here is not complete, there you should pass "my.package.name.fileprovider" as parameter some where.
In my case.
I have a library project .Let's call it :LibApk
When I code this : applicationId "my.package.name" in the build.grale(app)
when build gradle tell me that : Library projects cannot set applicationId.
applicationId is set to 'com.darcy.apkupdate' in default config.
So ,I delete applicationId "my.package.name" .
Then ,build.gradle look like this :
But I forgot update the AndroidManifest.xml file which use ${applicationId}
This is the problem!!!
So I changed the variable to constant .The result looks like below picture:
After This ,It's wokr for me!
Hope this is help you...
The problem here is, you use the class name .provider for authorities in manifest
and use .fileprovider class name in java code.
<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_app" />
</provider>
Couldn't find meta-data for provider with authority my.package.name.fileprovider
Just rename fileprovider to provider
If the build's suffix is different, it makes sense to make a change like this.
FileProvider.getUriForFile(mContext.get(), BuildConfig.APPLICATION_ID + ".fileprovider", file)
In My Case;
Manifest File:
<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>
Code:
import androidx.multidex.BuildConfig // NOT DO THIS!!!
val uri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID+ ".provider", _tempFile)
Exception:
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority androidx.multidex.provider
Do not use androidx.multidex.BuildConfig, Because this values are not the values of our application:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package androidx.multidex;
public final class BuildConfig {
public static final boolean DEBUG = false;
public static final String APPLICATION_ID = "androidx.multidex";
public static final String BUILD_TYPE = "release";
public static final String FLAVOR = "";
public static final int VERSION_CODE = -1;
public static final String VERSION_NAME = "";
public BuildConfig() {
}
}
{applicationId} == com.companyName.application
append ".provider"
which will be == com.example.test.provider
in xml authorities:com.example.test.provider
in activity Uri mPath = FileProvider.getUriForFile(this, "com.example.example.provider", imageFile);
FileProvider.getUriForFile(this, "{yourPAckageName}.fileprovider",
file);
to
FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", file);
I spent a day finding a solution. This part is very important. That saved my day.
mImageFromCamera = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", mImageFile);
android:authorities="${applicationId}.fileprovider"
authorities must be same in xml and in code
First you use
File(getExternalFilesDir(null),context?.getString(R.string.app_name));
instead of
Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name) // this is deprecated in API29
then use this
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
For more help: File Provider Description
You can replace your BuildConfig import class file name:
import androidx.multidex.BuildConfig;
with:
import com.yourAppName.BuildConfig;
This is Because of this authority name difference
<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 Look
<pre>android:authorities="${applicationId}.provider" </pre>
Here the authority name is you packagename.provider
(com.example.app.provider)
val uri: Uri? = FileProvider.getUriForFile(
this.contexts,
BuildConfig.APPLICATION_ID + ".provider",
file
)
whenever we copy paste the code the authority may be change from one source to another. in stackoverflow some devs put in manifest ${applicationId}.provider" and in uri they put (.fileprovider) FileProvider.getUriForFile(
this.contexts,
BuildConfig.APPLICATION_ID + ".fileprovider",
file
) this is the problem now authority is different that is showing in log
what solved my problem is :
replace your paths app file by this :
take a look at files root, i determined the path with my app package name, change the pkg name to yours.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/com.my.newproject4" />
<external-path
name="external_files"
path="." />
</paths>
and also in your provider
use your package name instead of ${applicationId}
same as me here
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.my.newproject4.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
this is what worked with me
the only solution and way after long time of trying and searching
just this was the problem
maybe it helps someone
i hope for you a good luck
Change the authorities to a unique name to solve the issue like
android:authorities="${applicationId}.myUniquefileprovider"
also in java code
I just removed the '$' from android:authorities="${applicationId}.provider" and it works like a charm now.
Its fileprovider and not provider
android:authorities="${applicationId}.fileprovider"
I'm trying to share file from remote location, but I'm getting the following exception:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/31AB-1310/Android/data/com.example.myapp/cache/EasyImage/a9c926ea-4dce-44b7-94e9-a5dca6b91a5d-450242437.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at pl.aprilapps.easyphotopicker.EasyImage.createCameraPictureFile(EasyImage.java:54)
at pl.aprilapps.easyphotopicker.EasyImage.createChooserIntent(EasyImage.java:109)
My FileProvider code has the following manifest entry:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_paths" />
</provider>
My res/xml/filpaths.xml 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>
My Activity.java file :
private void shareCurrentData(String filepath) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
ArrayList<Uri> files = new ArrayList<Uri>();
if (intent.resolveActivity(getPackageManager()) != null) {
try {
File shareFile= new File(filepath);
Uri shareUri =FileProvider.getUriForFile(this, AUTHORITY,shareFile);
//files.add(textUri);
files.add(shareUri);
} catch (Exception ex) {
System.out.println(ex);
}
intent.putExtra(Intent.EXTRA_STREAM, files);
startActivity(Intent.createChooser(intent,"Share using....."));
}
}
I just cant understand what raises the exception because everything seems to fit.
/storage/31AB-1310/Android/data/com.example.myapp/cache/EasyImage/a9c926ea-4dce-44b7-94e9-a5dca6b91a5d-450242437.jpg
A path like that usually comes from removable storage. FileProvider does not support removable storage.
So, focus on ensuring that filepath is on external storage, not removable storage.
I get this error: failed to find configured root that contains /storage/emulated/0/android/data/
There are some similar questions but they are using other location for storing files.
I am using:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) method and this is the code I'm using to open camera:
private void TakePicture() {
photoFile = FileManager.generateFileStampedPhotoFile();
PicturesHelper.TryTakePictureWithAnIntent(this, photoFile, REQUEST_IMAGE_CAPTURE);
}
public static File generateFileStampedPhotoFile()
{
Uri photoFileUri=null;
File photoFile=null;
File outputDir=getPhotoDirectory();
if(outputDir!=null)
{
String timeStamp=new SimpleDateFormat("yyyyMMDD_HHmmss").format(new Date());
String photoFileName="IMG_"+timeStamp+".jpg";
photoFile=new File(outputDir,photoFileName);
// photoFileUri=Uri.fromFile(photoFile);
}
return photoFile;
}
public static void TryTakePictureWithAnIntent(Activity context,File
photoFile,int requestCode)
{
Intent takePictureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(context.getPackageManager())!=null) //if user has camera?
{
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(context,
"com.myapp.fileprovider",
photoFile);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
context.startActivityForResult(takePictureIntent, requestCode);
}
}
}
public static File getPhotoDirectory()
{
File outputDir=null;
String externalStorageState=Environment.getExternalStorageState();
if(externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
outputDir=new File(new File(pictureDir,"Adriagate"),"Online");
if(!outputDir.exists())//V.P. if directory/ies not exist/s it will be created. mksdrs method follows file structure and creates directory by direcory if needed
{
if(!outputDir.mkdirs())
{
return null;
}
}
}
return outputDir;
}
I have put provider inside AndroidManifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"></meta-data>
</provider>
And this is file_paths.xml file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files name="Whatever_Nothing_Works"/>
You are storing your images in Environment.getExternalStoragePublicDirectory(). That is not one of the supported roots for FileProvider. Plus, your XML has <external-files>, and that is not a supported element for FileProvider (valid ones all end in -path).
Options include:
Changing the location of where you are storing the images to one of the locations supported by FileProvider and adjusting your XML to match
Switching to my StreamProvider and using <external-public-path dir="Pictures">
Changing your XML to <external-path name="whatever" path="Pictures"> and hope for the best
Add this in Menifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Replace in file_paths.xml with
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>
Your file is stored under getExternalFilesDir(). That maps to <external-files-path>, not <files-path>