android 6.0文件提供程序无法在FileProvider.getUriForFile上运行空指针

vim*_*liu 0 java android android-fileprovider

下面的代码用于尝试文件提供程序以准备android Nougat:

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.securefilesharing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="25" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeSecureFileShareTestActivity"
            android:label="@string/title_activity_home_secure_file_share_test" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.securefilesharing.fileprovider"
            android:enabled="false"
            android:grantUriPermissions="true" >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_path" />
        </provider>

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

文件提供程序xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)

构造URI并传递给相机意图:

private Uri createImageFile() throws Exception {
         File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), System.currentTimeMillis() + ".jpg");
         //File file = new File(System.currentTimeMillis() + ".jpg");

         Uri photoUri = null;

         try{
             photoUri = FileProvider.getUriForFile(this, "com.securefilesharing.fileprovider", file);
         }
         catch(Exception e){
             Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
         }

         return photoUri;
     }

View.OnClickListener ocl = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            try {
                uri = createImageFile();
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } catch (Exception e) {
                Log.e(TAG, TAG + ": " + e.toString());
            }           
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    };
Run Code Online (Sandbox Code Playgroud)

我还做了activity.requestPermissions(forPermissions.toArray(new String [0]),PERMISSION_CODE); 对于相机,读写外部文件

以下是异常消息:

06-22 15:04:13.962:E/HomeSecureFileShareTestActivity(31851):HomeSecureFileShareTestActivity:java.lang.NullPointerException:尝试调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content) .pm.PackageManager,java.lang.String)'在空对象引用上

Ash*_*ohn 5

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.securefilesharing.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true" >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_path" />
        </provider>
Run Code Online (Sandbox Code Playgroud)

更改清单中的提供商部分...请参阅:: https://developer.android.com/guide/topics/manifest/provider-element.html#enabled