java - 拍照并保存为 URI 时出现 NullPointerException
问题描述
我正在关注这篇博文,将此功能添加到我的应用程序中,我可以在其中拍照,然后将图片数据保存为 Uri 以获取更多功能。但是当我尝试打开相机意图时出现错误。
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)'
我怀疑有什么问题,file_paths.xml
但我仔细检查了没有问题。我已经对通过like 保存图像的不同方式进行了一些研究,FileProvider
但其中大部分对我也不起作用。
有谁知道我的代码有什么问题?
主要活动
private void openCameraIntent() {
Intent pictureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
if(pictureIntent.resolveActivity(getPackageManager()) != null){
//Create a file to store the image
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, com.example.camerapplication.provider", photoFile);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
photoURI);
startActivityForResult(pictureIntent,
REQUEST_CAPTURE_IMAGE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == REQUEST_CAPTURE_IMAGE && resultCode == RESULT_OK){
Glide.with(this).load(imageFilePath).into(image);
// TODO:Use Image Uri for further functions
}
}
private File createImageFile() throws IOException{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName,".jpg",storageDir);
imageFilePath = image.getAbsolutePath();
return image;
}
显现
<uses-feature android:name="android.hardware.camera" android:required="true" />
// ... Other elements and then
// Within application scope
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
文件路径.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/com.example.cameraapplication/files/Pictures" />
</paths>
已编辑 更改为正确的路径 =“Android/data/com.example.cameraapplication/files/Pictures”
解决方案
您的错误意味着您的应用程序 ID 不是com.example.android
.
在您的代码中,您有:
Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.provider", photoFile);
这意味着权限需要com.example.android.provider
。
在您的清单中,您有:
android:authorities="${applicationId}.provider"
因此,${applicationId}
不是com.example.android
。
你的android:authorities
价值很好,但你需要你的getUriForFile()
电话来匹配。一种典型的做法是:
Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", photoFile);
推荐阅读
- java - camunda spring boot starter with gradle 缺少 processEngineConfigurationImpl bean
- bash - 用于从 FTP 目录中删除超过 30 天的文件的 shell/bash 脚本
- c# - 时间:2019-05-10 标签:c# little坚持得到回报
- java - 我想在java中制作一个在特定时间停止的计时器
- apache-spark - Spark Read Json:如何读取在整数和结构之间交替的字段
- python - 如何结合两种或三种特征提取方法?
- reactjs - NavLink 在 React Router DOM 中的多个 URL 的同一链接上处于活动状态
- performance - Flutter:从 int 获取双精度更有效
- azure - 使用 azure cli --generate-ssh-keys 存储在 Windows 中的自动生成的 SSH 密钥在哪里
- excel - 如何根据多个电子邮件地址复制行?