error: can not find symbol variable FileProvider Android Studio 3

1

I am trying to adapt my Android application for systems 7.0.0+ it seems that one of the changes is using FileProvider, I am watching tutorials, it seems easy but it turns out that Android Studio 3 does not find this class and I am loading from gradle an android. support.v4 (where I think it's included) such that:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:support-v4:24.2.0'
    implementation 'com.android.support:design:24.2.0'
}

In the AndroidManifest.xml in the application I add the provider and I get the "FileProvider" class in red

<application
...
<provider
            android:name="android.support.v4.content.FileProvider"
            android:grantUriPermissions="true"
            android:exported="false"
            android:authorities="${applicationId}">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_paths"/>

        </provider>

...

And then when I try to use it in an activity it still does not recognize the class.

File file = new File(path + getResources().getString(R.string.downloadFile));
                                        if(Build.VERSION.SDK_INT <= 23){
                                            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                                        }
                                        else {
                                            intent.setDataAndType(Uri.fromFile(FileProvider.getUriForFile(this, "com.my.app", file)), "application/vnd.android.package-archive");
                                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                        }

What can I do? Thanks.

    
asked by Santiago 06.04.2018 в 17:51
source

2 answers

-1

You must add import so you can recognize the class FileProvider , the import is:

import android.support.v4.content.FileProvider;

This class does not depend on a dependency that you have to add in your build.gradle file, it is from the same Android SDK.

link

    
answered by 06.04.2018 / 18:24
source
0

Now I have another problem I skip an exception on the line:

intent.setDataAndType(Uri.fromFile(FileProvider.getUriForFile(this,"com.my.app", file)), "application/vnd.android.package-archive");

where com.my.app I replace it with my app package and it gives me this exception:

  

java.lang.IllegalArgumentException: Failed to find set root   that contains /storage/emulated/0/test/app.apk

If I check from a file browser that route exists.

My file file_provider_paths is this and the manifest does not give an error, it seems that it finds it

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path name="cache" path="/" />
    <external-files-path name="files" path="/" />
</paths>
    
answered by 08.04.2018 в 23:19