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

时间:2017-04-17 04:21:54

标签: android android-camera uri

当活动打开时,会打开一个对话窗口,要求用户从相机拍摄照片,或从图库中选择一个。一旦我按下按钮打开相机,应用程序崩溃,我得到一个与获取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();
            }
        });

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

清单:

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

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

2 个答案:

答案 0 :(得分:0)

  

尝试使用以下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;
}

答案 1 :(得分:0)

如果将来有人发现这个,我输入“provider”而不是“fileprovider”。由于提供者不存在,这是异常的来源。