Trying to open an image in the default gallery app. I can't resolve this issue following the steps:
My file path is /data/data/{applicationName}/cache/file.jpg
Intent:
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filePath);
intent.setDataAndType(FileProvider.getUriForFile(this, getApplicationContext()
.getPackageName() + ".provider", file),"image/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Manifest:
<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>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
I've tried changing the path and name in provider_paths but no luck.
Replace:
<external-path
name="external_files"
path="." />
with:
<cache-path name="external_files" />
as your path seems to be in getCacheDir().
Related
When I try to use
var apkURI = FileProvider.GetUriForFile(AndroidApp.Context, BuildConfig.ApplicationId + ".provider", file);
I get
Java.Lang.IllegalArgumentException: 'Couldn't find meta-data for provider with authority android.support.compat.provider'
android 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>
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>
I have no idea why i'm getting this error and all sources i try to find to help yields in no information to solve it, any help would be greatly appreciated
I am trying to open a pdf file which is stored on a internal storage but not able to view it. Below is my code snippet to save file on internal storage.
try (FileOutputStream fos = openFileOutput("demo.pdf", Context.MODE_PRIVATE)) {
fos.write(inputStream.read(buffer));
}
Code to open pdf file:
File file = new File(getFilesDir(), "demo.pdf");
Uri fileUri= FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".opener.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(photoURI, "application/pdf");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
opener_path.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
<cache-path name="cache" path="." />
<external-files-path name="external-files" path="." />
<external-cache-path name="external-cache" path="." />
<external-path name="external" path="." />
</paths>
In AndroidManifest.Xml
<provider
android:authorities="${applicationId}.opener.provider"
android:exported="false"
android:grantUriPermissions="true"
android:name="com.demo.fileopener2.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/opener_paths" />
</provider>
Any help will be appreciated. Thank you
I'm trying to install apk programmatically in "Work Profile" mode (BYOD). But there is a problem which I cannot solve : No way to install an apk in android versions 7.0(24) and 7.1(25). A pop-up dialog appear with this message :
Your administrator doesn't allow installation of apps obtained from
unknown sources
Voila how I do :
val uri = FileProvider.getUriForFile(context,authority,File(filePath))
val openFileIntent = Intent(Intent.ACTION_VIEW)
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
openFileIntent.data = uri
context.startActivity(openFileIntent)
in Manifest :
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_paths"
tools:replace="android:resource" />
</provider>
And file_provider_paths.xml :
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
</paths>
This is how I found out the solution :
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
devicePolicyManager.setSecureSetting(
componentName,
Settings.Secure.INSTALL_NON_MARKET_APPS,
"1"
)
} else {
devicePolicyManager.addUserRestriction(componentName,
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
}
This is my code block:
notificationIntent.setDataAndType(FileProvider.getUriForFile(
context,
"TrySomething",new File(context.getFilesDir().getAbsolutePath() + "/update_file")
),ANDROID_PACKAGE);
And this is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="myupdater" path="files/update_file"/>
</paths>
But i get this error:
java.lang.IllegalArgumentException: Failed to find configured root
that contains /data/data/com.example.trysomething/files/update_file
How can I fix it?
First Create Xml File inside src->xml folder Like
for example named file_paths_public.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
<root-path
name="external_files"
path="/storage/"/>
</paths>
then Add this Code in to your manifest file
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="<your app package>.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths_public" />
</provider>
now create one function that will support both api 26 and below device
that returns some valid uri
public static Uri getUriFromFile(Context theCtx, File theSrcPath) {
Uri requirdUri;
// Above Compile SDKversion: 25 -- Uri.fromFile Not working
// So we have to use Provider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
requirdUri = FileProvider.getUriForFile(theCtx,
theCtx.getApplicationContext().getPackageName() + ".provider",
theSrcPath);
} else {
requirdUri = Uri.fromFile(theSrcPath);
}
return requirdUri;
}
now use this uri Whenever you want
like
File FilePath = Environment.getExternalStorageDirectory().toString()/update_file/<here your file name >
Uri shareImageUri = getUriFromFile(this, <your full file Path Add hear>)
Check if you have this in your manifest
<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>
provider_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="directory_name" path="."/>
</paths>
Get file URI as
Uri fileUri = FileProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".provider", fileName(FILE))
I'm getting this exception when trying to use FileProvider:
Java.Lang.IllegalArgumentException: Unable to find configured root for content://com.abc/Android/data/com.abc/files/345345345.pdf Java.Lang.IllegalArgumentException
I guess my file_paths.xml configures the FileProvider with the wrong path. My code:
file_paths.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="." name="." />
<files-path path="com.abc/Android/data/com.abc/files/" name="files" />
</paths>
androidManifest.xml
<application android:label="#string/AppName" android:icon="#drawable/ic_launcher">
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.abc" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/file_paths" />
</provider>
</application>
What values should I put in path and name in file_paths.xml, to expose the file with the content URI: content://com.abc/Android/data/com.abc/files/345345345.pdf?
my code which creates the URI:
File newFile = new File (Application.Context.FilesDir.AbsolutePath, filename);
Android.Net.Uri androidContentUri = FileProvider.GetUriForFile (Application.Context, Application.Context.PackageName, newFile);
System.Uri systemContentUri = SystemUri (androidContentUri);
return systemContentUri;
Your entry:
<files-path path="com.abc/Android/data/com.abc/files/" name="files" />
Should become:
<files-path path="files" name="files" />
Path in this case represents the files in the files/ subdirectory of your app's internal storage area.
This application private subdirectory is the same as the value returned:
Application.Context.FilesDir;
So the file location within your app is:
var path = Path.Combine(Application.Context.FilesDir.AbsolutePath, "files");