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"
Related
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:
This is a part of my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asd"
android:versionCode="118"
android:versionName="118" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:name="com.example.asd.AsdApplication"
android:allowBackup="true"
android:allowTaskReparenting="true"
android:theme="#style/AsdTheme" >
...
<provider
android:name="com.example.asd.database.hq.ContentProviderDB"
android:authorities="ourContentProviderAuthorities" >
</provider>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.asd.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
...
</application>
</manifest>
This is the filepaths file in raw/xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="media"/>
</paths>
I download a video from internet and save it to internal storage this way:
public static boolean saveInputStreamToInternalStorageFile(Context context, String filename, byte[] dataToWrite, Context ctx) {
FileOutputStream fos;
try {
fos = new FileOutputStream(context.getFilesDir() + File.separator + filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(dataToWrite);
oos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
I try to use it like so:
private void playVideoFromDeviceWithWorkaround(String fileName) {
File newFile = new File(getFilesDir(), fileName);
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
try {
vvVideoFullscreen.setVideoURI(contentUri);
showMediaControls = true;
playVideo();
} catch (Exception e) {
playVideoFromNetwork();
}
}
At this line:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
The problem was that in Manifest I had this line:
android:authorities="com.example.asd.fileprovider"
and when calling getUriForFile I was passing:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
So changed from "com.example.asd" to "com.example.asd.fileprovider" and it worked
You can do this without hardcoding the package name with an additional benefit of being able to run multiple variants on the same device (think release and debug with applicationIdSuffix, see these issues):
Based on FileProvider.java:560
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData( //560
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
you were using the wrong authority and it didn't find the ContentProvider (info == null).
Change your manifest to (${applicationId} will be replaced by Manifest Merger)
android:authorities="${applicationId}.share"
and
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".share", result);
The .share suffix is optional, in case you have a real ContentProvider which is better to have the package name as the authority.
In my case, I got the error because the
BuildConfig.APPLICATION_ID
was being imported from
import android.support.v4.BuildConfig;
So the string it returned was "android.support.v4" instead of my project package name. Check out the import file is from your import project.Buildconfig and not another. Example:
import com.example.yourProjectName.BuildConfig;
Finally, in <provider> tag in Manifest I have android:authorities="${applicationId}" to always get my project package name as the authority
<manifest>
..
..
<application>
..
..
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/ruta_fileprovider" />
</provider>
</application>
</manifest>
First, be sure that you provider android:authorities does not conflict with your other providers. Besides that you may choose any name for the last part of its name: "provider", "fileprovider" etc., but app crashes when there are more than one android:authorities listed, while documentation states that it allows multiple values listed.
file:// scheme is now not allowed to be attached with Intent on targetSdkVersion >= 24 (Android N 7.0), only content:// is always passed for all devices (Android 5, 6 and 7). But we encountered that Xiaomi breaks this Google convention and sends file://, hence data.getData().getAuthority() gives empty string.
final String uriScheme = currentUri.getScheme();
if ("content".equals(uriScheme)) {
// getting full file path (works with some providers, i.e. Gallery)
path = FileUtils.getPath(getContext(), currentUri);
if (path != null) {
currentFile = new File(path);
}
} else if ("file".equals(uriScheme)) {
// in a rare case we received file:// in currentUri, we need to:
// 1. create new File variable from currentUri that looks like "file:///storage/emulated/0/download/50044382b.jpg"
// 2. generate a proper content:// Uri for it
currentFile = new File(currentUri.getPath());
String authority = data.getData().getAuthority();
if (authority != null && !authority.isEmpty()) {
currentUri = FileProvider.getUriForFile(getActivity(), authority, currentFile);
}
} else {
// throw exception
}
Also, the bug when FileProvider.getUriForFile() resulted in crash java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example/files/attachments/image.jpg was fixed in Android Support Library v24.2.0. The problem was that FileProvider.java did not see external-path folders.
If you're building your AUTHORITY at runtime using BuildConfig make sure you use the full class name including your package name.
Bad:
final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
Good:
final String AUTHORITY = com.mycompany.myapp.BuildConfig.APPLICATION_ID + ".provider";
Following worked for me.
mUri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider",
fileObject);
Here is what i did to fix the issue. I gave fully qualified name in android:name. It works in android 6,7,8
<provider android:authorities="${applicationId}.opener.provider"
android:exported="false" android:grantUriPermissions="true"
android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/provider_paths" />
</provider>
You should try it:
Context context = PostAdapter.this.activity;
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.append(PostAdapter.this.activity.getPackageName());
stringBuilder2.append(".provider");
Uri uri;
uri = FileProvider.getUriForFile(context,stringBuilder2.toString(), newFile);
This is what you need to do:
Uri fileURI = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", file);
I am creating a PDF file and saving it in local storage. When trying to open it, it is working perfect in all devices except in Android N. I am able to open PDF file in Android N using FileProvider, but it is displaying as blank.
This is my URI
content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
This is my code
Uri path;
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ "Android" + "/" + "data" + "/" + "com.products" + "/" + file);
if (Build.VERSION.SDK_INT >= 24) {
path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
} else {
path = Uri.fromFile(pdfFile);
}
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
The problem is with how you are setting the flags on the intent
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Instead, try this:
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Grzegorz Matyszczak's solution worked for my case. I had a very similar setup to Sabya Sachi. More specifically this line allowed me to see the PDF file (URI from FileProvider.getURIForFile call):
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
More info on grantUriPermissions here, under android:grantUriPermissions section.
As I can see that you have used FileProvider for Nougat.
You have to add a FileProvider tag in AndroidManifest.xml.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
</provider>
A FileProvider can only generate a content URI for files in directories that you specify beforehand.To link this file to the FileProvider, add a element as a child of the element that defines the FileProvider.
<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/file_paths"/>
</provider>
You must specify a child element of for each directory that contains files for which you want content URIs. You can add them to a new file called res/xml/file_paths.xml. For example ,these XML elements specify two directories:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="images/"/>
<files-path name="my_docs" path="docs/"/>
</paths>
->you have to set your PDF directory path in file_paths.xml
For more detail refer this
Replace FileProvider class with MyFileProvider class:
path = MyFileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
where MyFileProvider should be a generic class which extends FileProvider:
public class MyFileProvider extends FileProvider {
}
Modify AndroidManifest by setting your MyFileProvider class location to android:name attribute in your provider section:
android:name=".utils.MyFileProvider"
I'm sorry to ask an overly repeated question, but any of the answer here on stackoverflow really helped me.
I want to use a FileProvider to access file to a custom folder. My declaration in the manifest, inside the <application> tag:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
then, in provider_paths.xml
<paths>
<cache-path name="my_videos" path="MyFolderName/"/>
</paths>
(but I tried, instead of cache-path, files-path, external-path and external-files-path)
The creation of my files is:
public static Uri getUriFromFile(File file, Context context) {
int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
context.grantUriPermission(context.getPackageName(), uri, flags);
return uri;
}
public static File getMyDirectory() {
File root = new File(context.getCacheDir(), "MyFolderName");
if(root.mkdir()) Log.e(Consts.TAG, "Directory created");
return root;
}
...
File outputFile = new File(getMyDirectory(), filename + ".mp4");
videoUri = getUriFromFile(outputFile, this);
(and, instead of context.getCacheDir(), I tried context.getFilesDir(), Environment.getExternalStorageDirectory() and context.getExternalFilesDir(""))
and finally, I want to retrieve my file:
String pathToFile = "/my_videos/filename.mp4";
Uri file = getUriFromFile(new File(pathToFile), context);
tried with paths:
"/MyFolderName/filename.mp4"
"content://my.package.name.provider/my_videos/filename.mp4"
"content://my.package.name.provider/MyFolderName/filename.mp4"
ALL of this tries brought me to this error:
Failed to find configured root that contains /my_videos/filename.mp4
Any idea on what I am doing wrong? Thanks in advance!
I want to retrieve my file:
None of those are valid paths to the file.
You already have code that gives you a valid path to the file:
File outputFile = new File(getMyDirectory(), filename + ".mp4");
So, use the code that you already wrote.
This is a part of my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asd"
android:versionCode="118"
android:versionName="118" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:name="com.example.asd.AsdApplication"
android:allowBackup="true"
android:allowTaskReparenting="true"
android:theme="#style/AsdTheme" >
...
<provider
android:name="com.example.asd.database.hq.ContentProviderDB"
android:authorities="ourContentProviderAuthorities" >
</provider>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.asd.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
...
</application>
</manifest>
This is the filepaths file in raw/xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="media"/>
</paths>
I download a video from internet and save it to internal storage this way:
public static boolean saveInputStreamToInternalStorageFile(Context context, String filename, byte[] dataToWrite, Context ctx) {
FileOutputStream fos;
try {
fos = new FileOutputStream(context.getFilesDir() + File.separator + filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(dataToWrite);
oos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
I try to use it like so:
private void playVideoFromDeviceWithWorkaround(String fileName) {
File newFile = new File(getFilesDir(), fileName);
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
try {
vvVideoFullscreen.setVideoURI(contentUri);
showMediaControls = true;
playVideo();
} catch (Exception e) {
playVideoFromNetwork();
}
}
At this line:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
The problem was that in Manifest I had this line:
android:authorities="com.example.asd.fileprovider"
and when calling getUriForFile I was passing:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
So changed from "com.example.asd" to "com.example.asd.fileprovider" and it worked
You can do this without hardcoding the package name with an additional benefit of being able to run multiple variants on the same device (think release and debug with applicationIdSuffix, see these issues):
Based on FileProvider.java:560
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData( //560
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
you were using the wrong authority and it didn't find the ContentProvider (info == null).
Change your manifest to (${applicationId} will be replaced by Manifest Merger)
android:authorities="${applicationId}.share"
and
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".share", result);
The .share suffix is optional, in case you have a real ContentProvider which is better to have the package name as the authority.
In my case, I got the error because the
BuildConfig.APPLICATION_ID
was being imported from
import android.support.v4.BuildConfig;
So the string it returned was "android.support.v4" instead of my project package name. Check out the import file is from your import project.Buildconfig and not another. Example:
import com.example.yourProjectName.BuildConfig;
Finally, in <provider> tag in Manifest I have android:authorities="${applicationId}" to always get my project package name as the authority
<manifest>
..
..
<application>
..
..
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/ruta_fileprovider" />
</provider>
</application>
</manifest>
First, be sure that you provider android:authorities does not conflict with your other providers. Besides that you may choose any name for the last part of its name: "provider", "fileprovider" etc., but app crashes when there are more than one android:authorities listed, while documentation states that it allows multiple values listed.
file:// scheme is now not allowed to be attached with Intent on targetSdkVersion >= 24 (Android N 7.0), only content:// is always passed for all devices (Android 5, 6 and 7). But we encountered that Xiaomi breaks this Google convention and sends file://, hence data.getData().getAuthority() gives empty string.
final String uriScheme = currentUri.getScheme();
if ("content".equals(uriScheme)) {
// getting full file path (works with some providers, i.e. Gallery)
path = FileUtils.getPath(getContext(), currentUri);
if (path != null) {
currentFile = new File(path);
}
} else if ("file".equals(uriScheme)) {
// in a rare case we received file:// in currentUri, we need to:
// 1. create new File variable from currentUri that looks like "file:///storage/emulated/0/download/50044382b.jpg"
// 2. generate a proper content:// Uri for it
currentFile = new File(currentUri.getPath());
String authority = data.getData().getAuthority();
if (authority != null && !authority.isEmpty()) {
currentUri = FileProvider.getUriForFile(getActivity(), authority, currentFile);
}
} else {
// throw exception
}
Also, the bug when FileProvider.getUriForFile() resulted in crash java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example/files/attachments/image.jpg was fixed in Android Support Library v24.2.0. The problem was that FileProvider.java did not see external-path folders.
If you're building your AUTHORITY at runtime using BuildConfig make sure you use the full class name including your package name.
Bad:
final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
Good:
final String AUTHORITY = com.mycompany.myapp.BuildConfig.APPLICATION_ID + ".provider";
Following worked for me.
mUri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider",
fileObject);
Here is what i did to fix the issue. I gave fully qualified name in android:name. It works in android 6,7,8
<provider android:authorities="${applicationId}.opener.provider"
android:exported="false" android:grantUriPermissions="true"
android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/provider_paths" />
</provider>
You should try it:
Context context = PostAdapter.this.activity;
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.append(PostAdapter.this.activity.getPackageName());
stringBuilder2.append(".provider");
Uri uri;
uri = FileProvider.getUriForFile(context,stringBuilder2.toString(), newFile);
This is what you need to do:
Uri fileURI = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", file);