【问题标题】:Replace the file from URL to raw folder将文件从 URL 替换为原始文件夹
【发布时间】:2012-03-01 10:52:50
【问题描述】:

伙计们,我的 URL 中有一个文本文件。单击按钮后,我可以将其下载到 sdcard。

但我需要用原始文件夹中的文件替换下载的文件。两者是不同的文件。

这就是我从 URL 下载的方式

File sdcard = Environment.getExternalStorageDirectory();
File dir = new File (sdcard.getAbsolutePath() + "/varun");
dir.mkdirs();
try {
    u = new URL("http://hihowru.com/123.xml");
            file = new File(dir,"123.xml");
            startTime = System.currentTimeMillis();
                   Log.d("DownloadManager", "download begining");
                   Log.d("DownloadManager", "download url:" + url);
                   Log.d("DownloadManager", "downloaded file name:" + "a.mp3");
        URLConnection uconnection = u.openConnection();
        InputStream is = uconnection.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
                    baf = new ByteArrayBuffer(5000);
                       int current = 0;
                       while ((current = bis.read()) != -1) {
                          baf.append((byte) current);
                       }

                FileOutputStream fos;

                fos = new FileOutputStream(file);
                fos.write(baf.toByteArray());
                fos.flush();
                fos.close();

                Toast toast = Toast.makeText(getApplicationContext(), "Downloaded to Sdcard/varun"+audioxml, 0);
                toast.show();
                Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
                Intent ii = new Intent(DownloadFiles.this,Relaxation.class);
                startActivity(ii);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

但我需要用原始文件夹中的文件替换这个文件(下载的文件)

下载文件名:hi.txt 原始文件夹名称:hw.txt

如何实现,请帮忙

【问题讨论】:

标签: android file android-file


【解决方案1】:

您不能android resourcesasset 目录中修改或写入文件。因为android apk文件是read only。所以你只能阅读它。最好的方法是将该文件复制到internal storage 中,然后从该路径使用,同样在从 url 下载文件更新到内部存储路径之后。

更新:

将文件从 /asset 复制到应用程序内部存储的代码。

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

}

【讨论】:

  • 问题是我已经在应用程序中有一个默认文件我只需要替换下载的文件
  • 所以我可以复制到内部存储数据/数据...但是如何更新我的应用程序?请帮忙
  • 你不能。您必须将该文件复制到其他地方,然后从那里使用它。
  • 简要解释一下:我有一个活动,我从原始文件夹加载了一个默认文件。在该活动中,我有一个按钮单击按钮,它将显示从 URL 到下载的可用文件。当我单击其中一个,它将下载到 sdcard 到文件夹 varun 中。现在我需要做的是用默认文件替换下载的文件
  • 现在很简单,当您的应用程序第一次启动时,将您的可用文件放在 /asset 文件夹中在同一目录中。您也可以在文件目录中创建一个目录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 2015-11-13
  • 1970-01-01
相关资源
最近更新 更多