Android:如何从资产文件创建File对象?

时间:2021-07-23 02:54:38

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:

我在assets文件夹中有一个文本文件,我需要将其转换为File对象(而不是InputStream)。当我尝试这个时,我得到“没有这样的文件”例外:

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?

我可以修改它以使其工作吗?

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

顺便说一下,我尝试“按示例编写代码”查看我的项目中其他地方的代码,该代码引用assets文件夹中的HTML文件

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!

谢谢!

3 个解决方案

#1


24  

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.

您无法直接从资产获取File对象,因为资产不会存储为文件。您需要将资产复制到文件,然后在副本上获取File对象。

#2


8  

You cannot get a File object directly from an asset.

您无法直接从资产获取File对象。

First, get an inputStream from your asset using for example AssetManager#open

首先,使用例如AssetManager #open从资产中获取inputStream

Then copy the inputStream :

然后复制inputStream:

    public static void writeBytesToFile(InputStream is, File file) throws IOException{
    FileOutputStream fos = null;
    try {   
        byte[] data = new byte[2048];
        int nbread = 0;
        fos = new FileOutputStream(file);
        while((nbread=is.read(data))>-1){
            fos.write(data,0,nbread);               
        }
    }
    catch (Exception ex) {
        logger.error("Exception",ex);
    }
    finally{
        if (fos!=null){
            fos.close();
        }
    }
}

#3


-1  

This function missing in code. @wadali

代码中缺少此函数。 @wadali

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}

Source: https://*.com/a/4530294/4933464

资料来源:https://*.com/a/4530294/4933464

#1


24  

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.

您无法直接从资产获取File对象,因为资产不会存储为文件。您需要将资产复制到文件,然后在副本上获取File对象。

#2


8  

You cannot get a File object directly from an asset.

您无法直接从资产获取File对象。

First, get an inputStream from your asset using for example AssetManager#open

首先,使用例如AssetManager #open从资产中获取inputStream

Then copy the inputStream :

然后复制inputStream:

    public static void writeBytesToFile(InputStream is, File file) throws IOException{
    FileOutputStream fos = null;
    try {   
        byte[] data = new byte[2048];
        int nbread = 0;
        fos = new FileOutputStream(file);
        while((nbread=is.read(data))>-1){
            fos.write(data,0,nbread);               
        }
    }
    catch (Exception ex) {
        logger.error("Exception",ex);
    }
    finally{
        if (fos!=null){
            fos.close();
        }
    }
}

#3


-1  

This function missing in code. @wadali

代码中缺少此函数。 @wadali

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}

Source: https://*.com/a/4530294/4933464

资料来源:https://*.com/a/4530294/4933464