Error in FileProvider: java.lang.NullPointerException when using FileProvider.getUriForFile ()

1

I have a problem when trying to share a file with other applications using FileProvider, the problem comes when I use FileContent.getUriForFile() because it gives me 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

In my manifest I have declared my FileProvider in this way:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.example.myapp...  
    <application  
        ...  
        <Provider  
            android:name="android.support.v4.content.FileProvider"  
            android:authorities="${applicationId}.providers.FileProvider"  
            android:exported="false"  
            android:grantUriPermissions="true">  
            <meta-data  
                android:name="android.support.FILE_PROVIDER_PATHS"  
                android:resource="@xml/filepaths"/>  
        </Provider>  
    </application>  
</manifest>  

My filepaths.xml file within my res / xml directory is as follows:

<?xml version="1.0" encoding="utf-8"?>  
<Paths xmlns:android="http://schemas.android.com/apk/res/android">  
    <files-path name="downloaded" path="downloaded/"/>  
</Paths>

And within my class where I want to share a file I do the following:

public class ShareFiles extends AppCompatActivity {  
    // creo una variable global para acceder al authorities de mi Provider  
    private final static String MY_PROVIDER = BuildConfig.APPLICATION_ID + ".providers.FileProvider";

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        ....  
        FloatingActionButton compartir = (FloatingActionButton) findViewById(R.id.share_btn);  
        compartir.setOnClickListener(new View.OnClickListener() {  
            // creo y escribo texto en el archivo que quiero compartir  
            File dir = new File(Context.getFilesDir(), "downloaded");  
            // me aseguro de que el directorio exista y de no ser así lo creo  
            if(!dir.exists()) {  
                dir.mkdir();  
            }  
            // ahora creo el archivo dentro del directorio  
            File file = new File(dir, "mi_nuevo_archivo.txt");  
            // aquí realizo todo el proceso de escritura que omitiré para no confundir  
            // es en el siguiente paso donde me da el error  
            Uri contentFile = FileProvider.getUriForFile(getApplicationContext(), MY_PROVIDER, file);  
            ...  
        }  
    });  
}  

I do not understand well what is my error because according to the logcat line the error is due to trying to use the getUriForFile() method with a null object referring to one of the two parameters specified by the same text:

android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)'  

The issue is that by logic it refers to the android.content.pm.PackageManager object that by the way in which the getUriForFile() is constructed, this is derived from the context that is passed to it using getApplicationContext() .

Does anyone know where the error is or what am I doing wrong?
Thank you very much in advance

    
asked by Paris N. Salguero 14.02.2017 в 20:41
source

1 answer

1

The Android manifesto discriminates between lowercase and uppercase.

    <provider  
        android:name="android.support.v4.content.FileProvider"  
        android:authorities="${applicationId}.providers.FileProvider"  
        android:exported="false"  
        android:grantUriPermissions="true">  
        <meta-data  
            android:name="android.support.FILE_PROVIDER_PATHS"  
            android:resource="@xml/filepaths"/>  
    </provider>

already would be a good idea.

I also recommend that you use it to use

        boolean creado = dir.mkdirs();

instead of:

        if(!dir.exists()) {  
            dir.mkdir();  
        }

The folder is created only if it does not already exist.

    
answered by 14.02.2017 / 21:21
source