Unable to use FileProvider in Android studio - java

I am trying to use FileProvider for file authority (so that other apps could use that authority as their target for requests). The manifest looks like this:
<activity
android:name=".CrimePagerActivity"
android:parentActivityName=".CrimeListActivity">
</activity>
<provider
android:authorities="com.example.criminalintent.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.provider.FILE_PROVIDER_PATHS"
android:resource="#xml/files" />
</provider>
but when I try to build the app:
process: com.example.criminalintent, PID: 4598
java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data
at android.app.ActivityThread.installProvider(ActivityThread.java:7191)
...
What is andoird.support package (I have android.provider) and how can I get that package as dependency?

In your manifest entry, replace:
android:name="android.provider.FILE_PROVIDER_PATHS"
with:
android:name="android.support.FILE_PROVIDER_PATHS"

Related

How to set file provider for images stored in app data folder

I have been working on CameraX API and storing captured image on Internal storage App data folder with path say storage/emulated/0/Android/data/packageName/Files/IMG_25052021_171806.jpg
Now i want to get content URI for this saved image. For this i have been using provider in Manifest.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.abc.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepath" />
</provider>
with File path as
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="/" />
When i am calling FileProvider.getUriForFile(ImageCrop.this,"com.abc.xyz.fileprovider", pictureFile); it returning me Illegal Argument Exception
Please suggest !!!
You seem to be using getExternalFilesDir() for the filesystem location. If so, use <external-files-path>, not <files-path>, in your metadata XML resource. See the documentation for more.

Share content using ShareCompat

I have a strange problem when I try to share a my_file.html using ShareCompat.
As best practice I have created my own FileProvider but I get the error java.lang.IllegalArgumentException: Failed to find configured root that contains /forms/Proof_of_Life_Form.html I think I have set up all of the stuff correctly.
My XML I have created a provider for that.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.douglas.sample"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
This is my filepaths created in res/xml/filepaths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path path="/forms" name="forms" />
</paths>
and also my method that is responsible to call share content.
public void shareContent() {
File file = new File("/forms/", fileName + ".html");
Uri uri = FileProvider.getUriForFile(getContext(), "com.douglas.sample", file);
Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
.setType(getActivity().getContentResolver().getType(uri))
.setStream(uri)
.getIntent();
//Provide read access
shareIntent.setData(uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_form)));
}
Finally my exception:
java.lang.IllegalArgumentException: Failed to find configured root that contains /forms/Proof_of_Life_Form.html
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
at com.douglas.sample.TabbedFormsFragment$2.run(TabbedFormsFragment.java:254)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Any Idea? thanks :D
It seems there are some little mistakes in your code.
Try the following:
<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/provider_paths"/>
</provider>
and then in your provider_paths.xml try:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- We would like to give access to the External Storage of the app.
external_files will expose the path app to storage/emulated/0/ and the
"." is to actually point the root folder. The final path of the Uri will start
from path above and whatever folder we defined. For application upgrades
will be external_files/Android/data/package name/files/updates/name of update apk.-->
<external-path
name="external_files"
path="."/>
</paths>
and then whereever you want to use it try:
String path = Uri.parse(your_file_uri).getPath();
FileProvider.getUriForFile(application, PROVIDER, new File(path)),my_file_type);
where my_file_type = e.g. "application/vnd.android.package-archive" for .apk file
Hope it helps!!!

Multiple Google api keys in manifest file (Android)

I am trying to add two api keys i.e Google places api key and google maps api key.
But I am not able to add both at same time to manifest file.How can I achieve the same.Any help would be appreciated.
Thanks in advance.
The error I am getting is
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.geo.API_KEY in the element of AndroidManifest.xml
I am including api keys like this
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxx" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="xxxx"/>
Use following meta-data in your manifest. there is no need to create another api key for places api if you already have api key for google map.
If you are using both Maps & Places Api in your application then you only need to specify geo api key.and just enable Places Api.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="api_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Rather than adding two different API keys, for the same project enable both keys and copy down the same key as follows for single project which you want currently
<!-- Goolge Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaS******************WDaKCEHP" />
<!-- Google Places API Key -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaS******************WDaKCEHP" />
Whats your Error log saying
Caused by: java.lang.RuntimeException: The API key can only be
specified once. It is recommended that you use the meta-data tag with
the name: com.google.android.geo.API_KEY in the element of
AndroidManifest.xml
Just remove
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="xxxx"/>
Make sure you have below permissions . Enough for Map Showing .
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/common_google_api_key" />
<uses-library
android:name="com.google.android.maps"
android:required="false" />
Read
Getting error "java.lang.RuntimeException: Unable to start activity" in my app

how to solve IllegalStateException

I am developping an app and i wanna display a google map v2 on activity but it doesn't show
this is a snippet form logcat
11-30 19:14:56.681: E/AndroidRuntime(6062):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.cultural/com.example.cultural.MainActivity}:
android.view.InflateException: Binary XML file line #20: Error
inflating class fragment
11-30 19:14:56.681: E/AndroidRuntime(6062): Caused by:
java.lang.IllegalStateException: The meta-data tag in your app's
AndroidManifest.xml does not have the right value. Expected 6171000
but found 0. You must have the following declaration within the
element:
Make sure you add this to your manifest application tag :
<application
... >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR KEY HERE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
and see comment below or check this :
Android Google Maps API "Error inflating class fragment"
Make sure you have the following inside the <application> element in your AndroidManifest.xml.
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>

RuntimeException : Didn't find class on path: DexPathList

Yesterday everything worked fine with my project, but today I receive very weird errors :
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.redonbas/com.example.redonbas.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.redonbas.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.redonbas-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.redonbas-2, /vendor/lib, /system/lib]]
My AndroidManifest :
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
MainActivity is in correct package:
I refered to the problem but I copied all the File System of the project to the new, but that didn't help. I also created new project with another package and manually copied all the stuff from the old project there - the problem still remains!
What's wrong with it?
Uncheck your Android Dependencies and Android Private Libraries Checkboxes.
Yes, the problem was in past SherlockActionBar and its android-support library implementing .

Categories