简体   繁体   English

android-从资产文件夹复制文件

[英]android - copying files from asset folder

I need to copy all my files from within the assets folder of my app into the SD card so that I can write to this folder via updates from my web service. 我需要将我的应用程序的Assets文件夹中的所有文件复制到SD卡中,以便可以通过Web服务中的更新将其写入此文件夹。 However, I cannot seem to find the correct path. 但是,我似乎找不到正确的路径。 Below is the code I am currently using which works to copy a database over: 下面是我当前正在使用的代码,该代码可用于复制数据库:

public PluginResult execute(String arg0, JSONArray arg1, String arg2) 
{
    String[] resourseFileNames = {"file1.html","file2.html"};   
    final int length = resourseFileNames.length;

    try
    {
        String pName = this.getClass().getPackage().getName();
        this.copyOriginal("Databases.db","/data/data/"+pName+"/app_database/");
        this.copyOriginal("0000000000000001.db","/data/data/"+pName+"/app_database/file__0/");
        for(int i=0; i<length; i++)
        {
            this.copyOriginal("file:///android_asset/www/" + resourseFileNames[i],"file:///SDCard/MYAPP/www/" + resourseFileNames[i]);
        }
    }
    catch (IOException e)
    {
        System.err.println("Caught IOException: " + e.getMessage());
    }
}

void copyOriginal(String file, String folder) throws IOException 
 {
    File CheckDirectory;
    CheckDirectory = new File(folder);
    if (!CheckDirectory.exists())
    { 
    CheckDirectory.mkdir();
    }

    OutputStream out = new FileOutputStream(folder+file);
    InputStream in = this.ctx.getAssets().open(file);
    byte[] buf = new byte[1024];
    int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
    in.close(); 
    out.close();      
}

Any help here would be much appreciated, I believe I am close, its the file path I believe is wrong as I keep getting the error (logCat - 'my file path defienet' (No such file or directory). 在这里的任何帮助将不胜感激,我相信我已经很接近了,因为我不断收到错误消息(logCat-'我的文件路径defienet'(没有这样的文件或目录),我认为它的文件路径是错误的)。

Thanks 谢谢

It seemed that my above code was correct, the reason it was not working was due to the file size of my transfer was too big. 我的上述代码似乎是正确的,之所以无法使用,是因为我传输的文件太大。 Simplest solution was to upgrade my firmware to 2.3.3 + 最简单的解决方案是将固件升级到2.3.3 +

您的清单文件中有此行吗?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM