如何将文件写入资产文件夹或Android中的原始文件夹?

How to write files to assets folder or raw folder in android?

我正在某个应用程序上工作,该应用程序必须从一些http位置更新资产/原始文件夹运行时中存在的某些文件。

任何人都可以通过共享如何在资产或原始文件夹中写入文件来帮助我吗?


无法完成。是不可能的。


为什么不更新本地文件系统上的文件呢?
您可以将文件读/写到应用程序沙箱区域。

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

您可能要考虑的其他替代方法是"共享参考"和使用"缓存文件"(所有信息均在上面的链接中进行了介绍)


您无法将数据写入资产/原始文件夹,因为它是打包的(.apk)并且大小不可扩展。

如果您的应用程序需要从服务器下载依赖项文件,则可以使用android(http://developer.android.com/guide/market/expansion-files.html)提供的APK扩展文件。


解决同一问题的另一种方法可能会帮助您
在应用程序的私有上下文中读写文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
                 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 +"\
");
            }
            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();
    }
}

您在资产中时无法写入JSON文件。如前所述,资产是只读的。但是您可以将资产(json文件/资产中的其他任何内容)复制到移动设备的本地存储,然后从本地存储进行编辑(写入/读取)。还有更多存储选项,例如共享首选项(用于小数据)和sqlite数据库(用于大数据)。