Android 尝试获取文件的URI时获取空指针异常

Android 尝试获取文件的URI时获取空指针异常,android,android-camera,uri,Android,Android Camera,Uri,当活动打开时,将打开一个对话框窗口,要求用户从相机中拍照,或从“多媒体资料”中选择一张。当我按下按钮打开相机时,应用程序崩溃,我得到一个与获取URI相关的空指针异常。我一直在关注谷歌关于保存相机图片的演练,但似乎找不到问题所在 发生错误的行: Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this, "xyz.beerme.beerme.provider",

当活动打开时,将打开一个对话框窗口,要求用户从相机中拍照,或从“多媒体资料”中选择一张。当我按下按钮打开相机时,应用程序崩溃,我得到一个与获取URI相关的空指针异常。我一直在关注谷歌关于保存相机图片的演练,但似乎找不到问题所在

发生错误的行:

Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this,
                                "xyz.beerme.beerme.provider",
                                photoFile);
整体方法:

cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(intent.resolveActivity(getPackageManager()) != null){
                    File photoFile = null;
                    try{
                        photoFile = createImageFile();
                    } catch (IOException ex){
                        Snackbar message = Snackbar.make(findViewById(R.id.activity_create_post), "Error creating image", Snackbar.LENGTH_LONG);
                        message.show();
                    }
                    if(photoFile != null){
                        Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this,
                                "xyz.beerme.beerme.provider",
                                photoFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(intent, REQUEST_CAMERA);
                    }
                }
                dialog.dismiss();
            }
        });
private File createImageFile() throws IOException{
        String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_beerme";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );

        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
createImageFile方法:

cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(intent.resolveActivity(getPackageManager()) != null){
                    File photoFile = null;
                    try{
                        photoFile = createImageFile();
                    } catch (IOException ex){
                        Snackbar message = Snackbar.make(findViewById(R.id.activity_create_post), "Error creating image", Snackbar.LENGTH_LONG);
                        message.show();
                    }
                    if(photoFile != null){
                        Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this,
                                "xyz.beerme.beerme.provider",
                                photoFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(intent, REQUEST_CAMERA);
                    }
                }
                dialog.dismiss();
            }
        });
private File createImageFile() throws IOException{
        String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_beerme";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );

        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="xyz.beerme.beerme">

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:supportsRtl">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="redacted" />
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="xyz.beerme.beerme.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" ></meta-data>
        </provider>
        <activity android:name=".PostsActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.categroy.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".CreatePostActivity"></activity>
    </application>

</manifest>

文件\u paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/xyz.beerme.beerme/files/Pictures" />
</paths>

请尝试以下createImageFile方法的代码

请尝试以下createImageFile方法的代码


万一将来有人发现这个,我就输入“provider”而不是“fileprovider”。由于提供程序不存在,这就是异常的来源。

如果将来有人发现此问题,我键入“provider”而不是“fileprovider”。由于提供程序不存在,这就是异常的来源。

您可以将您的方法包含在try-catch子句中,然后打印异常,以查看该URI发生了什么。它肯定发生在我认为,这是erorr
java.lang.NullPointerException:尝试调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)'在空对象引用上
方法尝试运行的活动的名称为CreatePostActivity right?是的。我打开一个对话框会立即影响什么吗?可能会。尝试从
if(intent.resolveActivity(getPackageManager())!=null){
中排除所有
if(photoFile!=null){
以查看发生了什么(仅用于测试目的)。您可以将您的方法包含在try-catch子句中并打印异常,以查看该URI发生了什么。它肯定发生在我认为的行上,这是erorr
java.lang.NullPointerException:尝试调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData'(android.content.pm.PackageManager,java.lang.String)“”在空对象引用上
您的方法尝试运行的活动的名称是否为CreatePostActivity?是的。我打开一个对话框是否会立即影响任何内容?可能是。尝试从
if(photoFile!=null){
中排除所有
if(photoFile!=null){/code>(intent.resolveActivity(getPackageManager())!=null){
查看发生了什么(仅用于测试目的)。