I'm following this blog post to add this feature onto my app where I can take a picture then save the picture data as a Uri for further functions. But I'm getting an error when I try to open the camera intent.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
I suspect there is something wrong with the file_paths.xml
but I double checked there is nothing wrong. I've done some research on different ways of saving images through like FileProvider
but most of it doesn't work for me either.
Does anyone know what's wrong with my code?
MainActivity
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;
}
Manifest
<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>
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/com.example.cameraapplication/files/Pictures" />
</paths>
EDITED changed to proper path="Android/data/com.example.cameraapplication/files/Pictures"