如何从相机意图获取 URI?

我已经阅读了这篇文章:从 android 中的相机意图获取 uri但这对我没有帮助。


为了处理相机请求,我使用了以下代码:


    public void startCamera() {

    if (PermissionUtils.requestPermission(

            this,

            CAMERA_PERMISSIONS_REQUEST,

            Manifest.permission.READ_EXTERNAL_STORAGE,

            Manifest.permission.CAMERA)) {

        //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

        Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", getCameraFile());

        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivityForResult(intent, CAMERA_IMAGE_REQUEST);

    }

}



 public File getCameraFile() {

    File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    return new File(dir, FILE_NAME);

}



    @Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == RESULT_OK) {

        Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", getCameraFile());

        uploadImage(photoUri);

    }

}

当我调用函数“startCamera”时,应用程序崩溃并在行中出现错误:


Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", getCameraFile());"

有什么帮助吗?谢谢!


慕雪6442864
浏览 174回答 1
1回答

慕工程0101907

通过将以下内容添加到“AndroidManifest”来修复:&nbsp; &nbsp; &nbsp; &nbsp; <provider&nbsp; &nbsp; &nbsp; &nbsp; android:name="android.support.v4.content.FileProvider"&nbsp; &nbsp; &nbsp; &nbsp; android:authorities="${applicationId}.provider"&nbsp; &nbsp; &nbsp; &nbsp; android:exported="false"&nbsp; &nbsp; &nbsp; &nbsp; android:grantUriPermissions="true">&nbsp; &nbsp; &nbsp; &nbsp; <meta-data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:name="android.support.FILE_PROVIDER_PATHS"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:resource="@xml/provider_paths" />&nbsp; &nbsp; </provider>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java