安卓:我怎么创建资源文件的文件对象吗? [英] Android: how do I create File object from asset file?

查看:153
本文介绍了安卓:我怎么创建资源文件的文件对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在,我需要变成一个File对象(不进的InputStream)资产文件夹中的文本文件。当我尝试这样做,我得到了没有这样的文件异常:

I have a text file in the assets folder that I need to turn into a File object (not into InputStream). When I tried this, I got "no such file" exception:

String path = "file:///android_asset/datafile.txt";
URL url = new URL(path);
File file = new File(url.toURI());  // Get exception here

我可以修改这个来得到它的工作?

Can I modify this to get it to work?

顺便说一句,我那种试图code。通过例如望着下面这段$ C $在我的项目的任何位置C,它引用了资产的文件夹中的HTML文件

By the way, I sort of tried to "code by example" looking at the following piece of code elsewhere in my project that references an HTML file in the assets folder

public static Dialog doDialog(final Context context) {
WebView wv = new WebView(context);      
wv.loadUrl("file:///android_asset/help/index.html");

我承认,我不完全理解上述机制,所以它可能是什么,我试图做是行不通的。

I do admit that I don't fully understand the above mechanism so it's possible that what I am trying to do can't work.

THX!

推荐答案

您无法从资产获得文件对象直接,因为该资产不存储为文件。您需要将资产复制到文件,然后得到一个文件对象的副本。

You cannot get a File object directly from an asset, because the asset is not stored as a file. You will need to copy the asset to a file, then get a File object on your copy.

这篇关于安卓:我怎么创建资源文件的文件对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆