简体   繁体   English

如何将文件写入Android中的Assets文件夹或Raw文件夹?

[英]How to write files to assets folder or raw folder in android?

I am working on some application where I have to update some files present in assets / raw folder runtime from some http location. 我正在某个应用程序上工作,该应用程序必须从一些http位置更新资产/原始文件夹运行时中存在的某些文件。

Can anyone help me to by sharing how to write files in assets or raw folder? 谁能通过共享如何在资产或原始文件夹中写入文件来帮助我?

It cannot be done. 无法完成。 It is impossible. 是不可能的。

Why not update the files on the local file system instead? 为什么不更新本地文件系统上的文件呢? You can read/write files into your applications sandboxed area. 您可以将文件读/写到应用程序沙箱区域。

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal http://developer.android.com/guide/topics/data/data-storage.html#files内部

Other alternatives you may want to look into are Shared Perferences and using Cache Files (all described at the link above) 您可能要考虑的其他替代方法是“共享参考”和使用“缓存文件”(所有信息均在上面的链接中进行了介绍)

You cannot write data's to asset/Raw folder, since it is packed( .apk ) and not expandable in size. 您无法将数据写入资产/原始文件夹,因为它是打包的( .apk )并且大小不可扩展。

If your application need to download dependency files from server, you can go for APK Expansion Files provided by android ( http://developer.android.com/guide/market/expansion-files.html ). 如果您的应用程序需要从服务器下载依赖项文件,则可以使用android( http://developer.android.com/guide/market/expansion-files.html )提供的APK扩展文件。

Another approach for same issue may help you Read and write file in private context of application 针对同一问题的另一种方法可能会帮助您在应用程序的私有上下文中读取和写入文件

                 String NOTE = "note.txt";  
                 private void writeToFile() {
        try {
         OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
                NOTES, 0));

         out.write("testing");
         out.close();
         }

        catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
        }
             }


           private void ReadFromFile()
      {
        try {
        InputStream in = openFileInput(NOTES);
        if (in != null) {
            InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(tmp);
            String str;
            StringBuffer buf = new StringBuffer();
            while ((str = reader.readLine()) != null) {
                buf.append(str + "\n");
            }
            in.close();
            String temp = "Not Working";
            temp = buf.toString();
            Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
        }
    } catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
    }
}

You Can't write JSON file while in assets. 您在资产中时无法写入JSON文件。 as already described assets are read-only. 如前所述,资产是只读的。 But you can copy assets (json file/anything else in assets ) to local storage of mobile and then edit(write/read) from local storage. 但是您可以将资产(json文件/资产中的其他任何内容)复制到移动设备的本地存储,然后从本地存储进行编辑(写入/读取)。 More storage options like shared Preference(for small data) and sqlite database(for large data) are available. 还有更多存储选项,例如共享首选项(用于小数据)和sqlite数据库(用于大数据)。

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

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