【发布时间】:2021-06-09 21:26:50
【问题描述】:
我是 android studio 的新手,我正在尝试基于 this 教程构建一个应用程序,但由于某种原因我的应用程序崩溃了。我搜索并尝试了很多想法,但仍然没有进展。
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我使用这些权限,提供者看起来像这样。
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.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_path.xml
<?xml version="1.0" encoding="utf-8"?
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/example.com.camera/files/Pictures" /
</paths>
我还创建了与教程中相同的图像视图和捕获按钮。 我尝试调试它,发现这个函数发生了错误,并且代码卡在 if 语句上并且没有传递它。我发现了一些有助于通过 if(将 if 更改为这有助于 getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) 但随后应用程序再次在 FileProvider.getUriForFile() 上崩溃...
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (cameraIntent.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
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
代码只是该教程的复制粘贴,我只创建了视图和按钮。 那么请问大家有没有遇到过类似的情况呢?如果有人能帮我解决这个问题,我将不胜感激。
【问题讨论】:
标签: android android-intent camera