我想打开相机并从相机拍摄照片以将其上传到服务器。但是,每次我尝试打开相机时,应用程序崩溃都会显示上述错误,我尝试了多种解决方案,但它们对我都不起作用。我的代码如下:
我的包裹名称是
lc.android.gauge
并且应用程序ID为
com.test.gauge
授予权限后,我称为此方法
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
try {
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.test.gauge.fileprovider",
file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 0);
}
} catch (Exception e) {
Log.e("imageException", e.toString());
}
}
}
应用程序在上述方法中在此行崩溃
Uri photoURI = FileProvider.getUriForFile(this,"com.test.gauge.fileprovider",file);
createImageFile()方法
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
内部清单标签
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
内部应用程序标记
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="lc.android.gauge.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths xml
<paths>
<external-files-path
name="my_images"
path="Android/data/com.test.gauge/files/Pictures" />