【问题标题】:NullPointerException when trying to get URI with FileProvider(Android) [duplicate]尝试使用 FileProvider(Android) 获取 URI 时出现 NullPointerException [重复]
【发布时间】:2017-09-11 12:22:56
【问题描述】:

我想在点击按钮后拍照。但是当我尝试使用FileProvider 获取URI 时,我得到了NullPointerException

这是我的错误代码:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
        at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)

    at ibas.orosol.BildActivity.takePicture(BildActivity.java:86)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:5637) 
    at android.view.View$PerformClick.run(View.java:22429) 
    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:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

  

问题线似乎是:

mUri = FileProvider.getUriForFile(getApplication().getApplicationContext(),
            "ibas.provider", file);

这是我的代码

public void takePicture(View view) throws IOException {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try{
        File file =  createImageFile();

        mUri = FileProvider.getUriForFile(getApplication().getApplicationContext(),
                "ibas.provider", file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    } catch (IOException e){
        e.printStackTrace();
    }

}


private File createImageFile() throws IOException {

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    String path = "sdcard/orosol/captured_image.jpg";
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        Log.i("uri-data", "Uri: " + mUri.toString());


        mBitmap = getBitmapFromUri(mImageView,BildActivity.this,mUri);


        mImageView.setImageBitmap(mBitmap);
        mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    }

}

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ibas.orosol">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="ibas.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MenuActivity" />
    <activity
        android:name=".HeizoelActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MenuActivity" />
    </activity>
    <activity
        android:name=".BildActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MenuActivity" />
        >
    </activity>
    <activity
        android:name=".FacebookActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MenuActivity" />
    </activity>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".KontakteActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MenuActivity" />
    </activity>
    <activity android:name=".UeberUnsActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MenuActivity" />
    </activity>
</application>

res/xml 中的提供者

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/> </paths>

知道问题出在哪里吗?

【问题讨论】:

  • 你是否在 res/xml 文件夹中添加了 xml 文件?
  • 是的,我已经添加了。还是一样的问题。
  • 发布它........
  • 发布您的完整清单会有所帮助
  • getApplication().getApplicationContext() == null ?还是 file== null ?使用前检查。

标签: android image android-intent nullpointerexception uri


【解决方案1】:

提供者标签在清单文件中的应用标签之外。

您应该将它添加到&lt;application&gt;&lt;/application&gt; 标签内

 <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="ibas.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

【讨论】:

  • 我在 res 下创建了 xml 文件夹并添加了 provider_paths.xml 文件。然后我将提供包含在应用程序中,但在“@xml/provider_paths”上显示错误
  • 你是赢家,谢谢。 Android 上的示例显示点点滴滴,缺乏完整的代码,除非您进入示例代码,否则您无法发现这些问题。
  • 你是我的救命先生!
  • 我已经这样做了,但我仍然收到错误。
  • @wesleyfranks 检查您传递给 getUriForFile 的权限是否与清单中声明的​​权限相同;如果它不同,你也会得到同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 2012-06-19
相关资源
最近更新 更多