FileProvider.getUriForFile 上的空指针

问题描述 投票:0回答:1

我正在尝试让我的旧应用程序再次工作...当尝试使用以下代码捕获图片时:

public void photoIntent() {
        Log.d("MainActivity", "photoIntent");
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = null;
        try {
            File outputDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); // context being the Activity pointer
            photo = new File(outputDir+"picture.jpg");
            // place where to store camera taken picture
            //photo.delete();
        } catch (Exception e) {
            Log.d("MainActivity", "Can't create file to take picture!");
        }
        **Uri mImageUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider, photo);**
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
        // start camera intent
        this.startActivityForResult(intent, 1);
    }

我在“mImageUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, photo);”行上遇到此错误:

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
                                                                                                        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 com.marzinp.mycelium.MainActivity.photoIntent(MainActivity.java:96)
                                                                                                        at com.marzinp.mycelium.MainActivity$1.onClick(MainActivity.java:69)
                                                                                                        at android.view.View.performClick(View.java:7892)
                                                                                                        at android.widget.TextView.performClick(TextView.java:16220)
                                                                                                        at android.view.View.performClickInternal(View.java:7869)
                                                                                                        at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
                                                                                                        at android.view.View$PerformClick.run(View.java:30891)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:226)
                                                                                                        at android.os.Looper.loop(Looper.java:313)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8762)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.marzinp.mycelium"
    android:versionCode="112"
    android:versionName="1.1.2" >

    <uses-sdk
        android:minSdkVersion="22"
        android:targetSdkVersion="33" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/lombric"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.marzinp.mycelium.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

        <activity
            android:name="com.marzinp.mycelium.MainActivity"
            android:configChanges="orientation|screenSize"         
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

和我的 res.xmlprovider_path 文件:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="Pictures" />
</paths>

我尝试了很多解决方案,从很多网页,都没有结果......

感谢您的帮助,我是菜鸟,10年后重返android!

android capture
1个回答
0
投票

我修改了清单并将错误行更正为: ... String auth=BuildConfig.APPLICATION_ID+".fileprovider";尝试 { Uri mImageUri = FileProvider.getUriForFile(this, auth, photo); intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); // 启动相机意图 this.startActivityForResult(intent, 1); }catch (Exception e) { Toast.makeText(getApplicationContext(), "getUriForFile 时出错。", Toast.LENGTH_LONG).show();' } ... 仍然遇到同样的错误... – 皮埃尔·M

© www.soinside.com 2019 - 2024. All rights reserved.