gpt4 book ai didi

java - 如何在Android中正确获取文件的URI?

转载 作者:行者123 更新时间:2023-12-02 03:06:29 24 4
gpt4 key购买 nike

我正在尝试获取如下文件的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com