简体   繁体   English

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

[英]Getting null pointer exception when trying to get the URI for a file

When the activity opens, a dialog window opens asking the user to either take a picture from the camera, or choose one from the gallery. 活动打开后,将打开一个对话窗口,要求用户从相机拍摄照片,或从图库中选择一个。 As soon as I hit the button to open the camera, the app crashes and I get a Null Pointer exception related to getting the URI. 一旦我按下按钮打开相机,应用程序崩溃,我得到一个与获取URI相关的空指针异常。 I have been following Google's walkthrough for saving pictures form the camera and can't seem to find where the issue is. 我一直在关注谷歌的演练,从相机中保存图片,似乎无法找到问题所在。

Line where the error occurs: 发生错误的行:

Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this,
                                "xyz.beerme.beerme.provider",
                                photoFile);

Whole method: 整个方法:

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();
            }
        });

createImageFile method: createImageFile方法:

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;
    }

Manifest: 表现:

<?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>

file_paths.xml file_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>

Try Below code for createImageFile method 尝试下面的createImageFile方法代码

  private File createImageFile() {
    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    //  path = path + (timeStamp + "1jpg");

    try {
        file = File.createTempFile(timeStamp, ".jpg", storageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (Build.VERSION.SDK_INT >= 24)
        mCurrentPhotoPath = String.valueOf(FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", file));
    else
    mCurrentPhotoPath = String.valueOf(Uri.fromFile(file));
    return file;
}

In case anyone finds this in the future, I typed "provider" instead of "fileprovider". 如果将来有人发现这个,我输入“provider”而不是“fileprovider”。 As provider does not exist, that is the source of the exception. 由于提供程序不存在,这是异常的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM