作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取如下文件的 URI:
Uri photoURI = FileProvider.getUriForFile(this,
"br.com.[blablabla].sgaconsultor",
createImageFile());
private File createImageFile() throws IOException {
// Create an image file name
String imageFileName = "registroFoto";
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
directory
);
}
我有一个文件提供程序:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="br.com.[blablabla].blabla"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
XML 是:
<paths>
<files-path name="br.com.[blablabla].blabla" path="app_imageDir" />
</paths>
但是当我运行此代码时,我总是收到此错误:
java.lang.IllegalArgumentException: Android: FileProvider IllegalArgumentException Failed to find configured root that contains /data/data/**/files/Videos/final.mp4
但到目前为止还没有运气......那么我该如何解决这个问题并获取我的文件的 URI?
非常感谢任何帮助!
最佳答案
So how can I fix this and get the URI for my file?
第 1 步:去掉 Java 代码中无用的东西。
第 2 步:获取要匹配的 Java 代码和 file_paths
资源,因为 file_paths
表示文件路径必须位于 getFilesDir()
并且其中有 app_imageDir
,而您的 Java 代码没有这些东西。
所以,试试这个:
private File createImageFile() throws IOException {
// Create an image file name
String imageFileName = "registroFoto";
File directory = new File(getFilesDir(), "app_imageDir");
directory.mkdirs();
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
directory
);
}
关于java - 如何在Android中正确获取文件的URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41640547/
我是一名优秀的程序员,十分优秀!