如何从资产文件中获取 URI?
我一直在尝试获取资产文件的 URI 路径。
uri = Uri.fromFile(new File("//assets/mydemo.txt"));
当我检查文件是否存在时,我看到该文件不存在
File f = new File(filepath);
if (f.exists() == true) {
Log.e(TAG, "Valid :" + filepath);
} else {
Log.e(TAG, "InValid :" + filepath);
}
有人可以告诉我如何提及资源文件夹中存在的文件的绝对路径
I have been trying to get the URI path for an asset file.
uri = Uri.fromFile(new File("//assets/mydemo.txt"));
When I check if the file exists I see that file doesn't exist
File f = new File(filepath);
if (f.exists() == true) {
Log.e(TAG, "Valid :" + filepath);
} else {
Log.e(TAG, "InValid :" + filepath);
}
Can some one tell me how I can mention the absolute path for a file existing in the asset folder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
不存在“资产文件夹中存在的文件的绝对路径”。项目的
assets/
文件夹的内容打包在 APK 文件中。使用AssetManager
对象来获取资产上的InputStream
。对于
WebView
,您可以像使用 URL 一样使用file
Uri
方案。资产的语法为file:///android_asset/...
(注意:三个斜杠),其中省略号是assets/
文件夹中文件的路径。There is no "absolute path for a file existing in the asset folder". The content of your project's
assets/
folder are packaged in the APK file. Use anAssetManager
object to get anInputStream
on an asset.For
WebView
, you can use thefile
Uri
scheme in much the same way you would use a URL. The syntax for assets isfile:///android_asset/...
(note: three slashes) where the ellipsis is the path of the file from within theassets/
folder.正确的 url 是:
其中 RELATIVEPATH 是资源相对于 asset 文件夹的路径。
注意方案中的 3 /。如果没有 3,Web 视图将不会加载我的任何资源。我尝试了 CommonsWare(之前)评论的 2,但它不起作用。然后我查看了 github 上的 CommonsWare 源代码,注意到了额外的正斜杠。
虽然此测试仅在 1.6 Android 模拟器上完成,但我怀疑它在真实设备或更高版本上是否有所不同。
编辑:CommonsWare 更新了他的答案以反映这一微小的变化。所以我编辑了这个,这样它对于他当前的答案仍然有意义。
The correct url is:
where RELATIVEPATH is the path to your resource relative to the assets folder.
Note the 3 /'s in the scheme. Web view would not load any of my assets without the 3. I tried 2 as (previously) commented by CommonsWare and it wouldn't work. Then I looked at CommonsWare's source on github and noticed the extra forward slash.
This testing though was only done on the 1.6 Android emulator but I doubt its different on a real device or higher version.
EDIT: CommonsWare updated his answer to reflect this tiny change. So I've edited this so it still makes sense with his current answer.
最后,我找到了一种从 Kotlin 中的 this 答案获取资产中存在的文件路径的方法。在这里,我们将资产文件复制到缓存并从该缓存文件获取文件路径。
获取文件的路径,例如:
Finally, I found a way to get the path of a file which is present in assets from this answer in Kotlin. Here we are copying the assets file to cache and getting the file path from that cache file.
Get the path to the file like:
请尝试此代码工作正常
Please try this code working fine
确保您的资源文件夹放置在正确的位置。
Be sure ,your assets folder put in correct position.
适用于 WebView,但似乎在
URL.openStream()
上失败。因此,您需要区分 file:// 协议并按照建议通过 AssetManager 处理它们。Works for WebView but seems to fail on
URL.openStream()
. So you need to distinguish file:// protocols and handle them via AssetManager as suggested.试试这个,它有效:
如果您收到空值异常,请尝试这个(使用类
TopBrandData
):Try this out, it works:
If you get a Null Value Exception, try this (with class
TopBrandData
):试试这个:
我已经做到了并且有效
try this :
I had did it and it worked
由于它实际上是 APK 文件中的数据而不是模拟器上的数据,因此您将无法找到“绝对路径”。我最终以这种非常简单的方式实现了:
Since it's actually data in the APK file and not on the emulator, you won't be able to find an "absolute path". I've eventually implemented in this pretty straightforward way :
是的,您无法从 Android 手机或模拟器访问驱动器文件夹,因为您的计算机和 Android 是两个不同的操作系统。我会选择 Android 的 res 文件夹,因为它具有良好的资源管理方法。除非您有充分的理由将文件放入资产文件夹中。相反你可以这样做
Yeah you can't access your drive folder from you android phone or emulator because your computer and android are two different OS.I would go for res folder of android because it has good resources management methods. Until and unless you have very good reason to put you file in assets folder. Instead You can do this
如果您可以不使用 asset 文件夹,并且希望获取 URI 而不将其存储在其他目录中,则可以使用 res/raw 目录并创建一个辅助函数来从 resID 获取 URI:
现在,如果您在
res/raw
目录下有一个 mydemo.txt 文件,您可以通过调用上述帮助器方法来获取 URI参考:https://stackoverflow.com/a/57719958
If you are okay with not using assets folder and want to get a URI without storing it in another directory, you can use
res/raw
directory and create a helper function to get the URI from resID:Now if you have a mydemo.txt file under
res/raw
directory you can simply get the URI by calling the above helper methodReference: https://stackoverflow.com/a/57719958