我一直在研究一个简单的Android应用程序,它可以拍照并将其存储在应用程序的目录中。我完全按照Android Developpers教程进行了操作,但我仍然遇到错误。
我在StackOverflow中看到很多关于此错误的问题,但没有人看到修复我的错误。
这是我得到的logcat错误:
FATAL EXCEPTION: main
Process: com.example.victormaruca.gpstesting, PID: 31539
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.victormaruca.gpstesting/files/Pictures/JPEG_20170914_014854_-551864997.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)
at com.example.victormaruca.gpstesting.MainActivity.dispatchTakePictureIntent(MainActivity.java:124)
at com.example.victormaruca.gpstesting.MainActivity.access$000(MainActivity.java:43)
at com.example.victormaruca.gpstesting.MainActivity$3.onClick(MainActivity.java:81)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
这是我调用getUriForFile的方法:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.e("Erro", "no ceateImageFile() : "+ex);
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.victormaruca.gpstesting.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
这是我创建文件的方法:
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;
}
AndroidManifest.xml片段:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.victormaruca.gpstesting.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"
android:required="true" />
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.victormaruca.gpstesting/files/Pictures/" />
</paths>
任何帮助都会被贬低!
答案 0 :(得分:1)
解决方案
将您的 file_paths.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="Pictures" />
</paths>
其他内容
我想您关注官方文章to take photos on Android的情况。此处的示例 res / xml / file_paths.xml 令人困惑。福克斯的例子:
<!-- Not working. According to the docs. -->
<external-files-path
name="my_images"
path="Android/data/com.example.package.name/files/Pictures" />
<!-- Works -->
<external-files-path
name="my_images"
path="Pictures" />
<!-- Also works -->
<external-files
name="my_images"
path="Android/data/com.example.package.name/files/Pictures" />
我找到了解决方法here in the video。
另外,看看该方法的源代码 您使用的 FileProvider $ SimplePathStrategy.getUriForFile 。在调试源代码时,您可以看到它如何构建Uri以及为什么它可能引发IllegalArgumentException。它也可以导致解决方案。
源代码:
...
// Find the most-specific root path
Map.Entry<String, File> mostSpecific = null;
for (Map.Entry<String, File> root : mRoots.entrySet()) {
final String rootPath = root.getValue().getPath();
if (path.startsWith(rootPath) && (mostSpecific == null
|| rootPath.length() > mostSpecific.getValue().getPath().length())) {
mostSpecific = root;
}
}
if (mostSpecific == null) {
throw new IllegalArgumentException(
"Failed to find configured root that contains " + path);
}
...