FileProvider.getUriForFile返回null

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

我正试图在我的设备的相机中拍照,当我调用处理图像捕获的方法时,由于null指针异常,应用程序崩溃。我不知道我的FileProvider缺少什么信息,因为stacktrace将nullpointer指向我的FileProvider.getUriForFile语句。

这是我的代码HomeActivity

public void takePhoto(View view){
        //camera stuff
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        //folder stuff
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "AnimEncylopedia");
        if(!imagesFolder.exists()) {
            imagesFolder.mkdirs();
        }

        File image = new File(imagesFolder, "QR_" + timeStamp + ".png");


 Uri uriSavedImage = FileProvider.getUriForFile(HomeActivity.this, "com.encyclopedia.fileprovider", image);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        startActivityForResult(intent, 1);
    }

AndroidManifest

<provider
            android:authorities="com.encyclopedia.fileprovider"
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>

        </provider>

Files_path.xml

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

我怎样才能解决这个问题?

android xml image android-fileprovider
1个回答
0
投票

使用相机意图

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CAMERA);

onActivityResult()

Bitmap photo = (Bitmap) data.getExtras().get("data");

Uri tempUri = getImageUri(getActivity(), photo);

它还取决于用户使用的相机应用程序。使用Android相机,它将工作正常。但对于其他应用程序,它取决于它们是否返回有效的数据值。

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