我看了很多这样的帖子,但还是不知道问题出在哪里。我尝试更改 file_paths.xml 中的路径和名称。
文件路径.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="pirosfogo_images"
path="storage/emulated/0/pictures/"/>
</paths>
AndroidManifest.xml:
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true"/>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</application>
java:
void takePhoto(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 110);
}
else
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null)
{
File photoFile = createPhotoFile();
if(photoFile != null){
pathToFile = photoFile.getAbsolutePath();
Uri photoURI = FileProvider.getUriForFile(ProfileEdit.this, "adada", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
}
}
private File createPhotoFile() {
String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = null;
try{
image = File.createTempFile(name, ".jpg", storageDir);
} catch(Exception e){}
return image;
}
为什么我会收到错误消息?
这是因为您指定了<meta-data>
标记错误的父级(<application>
标签)。应在规定范围内指定<provider>
标签。 (下面的两个代码片段显示了差异:)
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
VS
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true"/>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</application>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)