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


73

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

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


4
资产/原始文件夹在电话上不存在。它们被打包到程序包中
Falmarri 2010年

Answers:


143

无法完成。是不可能的。


5
实际上,我是通过视图故障完成的(Web视图显示了我的Java源代码,然后从资产加载的html文件包含我的Java源代码,没有空格或任何东西)。我目前正在寻找如何重新创建它。
CQM



1

针对同一问题的另一种方法可能会帮助您在应用程序的专用上下文中读取和写入文件

                 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();
    }
}

0

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.