0

URLにテキストファイルがあります。ボタンをクリックすると、SDカードにダウンロードできます。

しかし、ダウンロードしたファイルを raw フォルダー内のファイルに置き換える必要があります。どちらも別のファイルです。

これは私が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();
            }

しかし、このファイル(ダウンロードしたファイル)をrawフォルダーのファイルに置き換える必要があります

ダウンロードファイル名:hi.txt 生フォルダ名:hw.txt

これを達成する方法を助けてください

4

1 に答える 1

0

android resourcesまたはディレクトリ内のファイルを変更または書き込むことはできませんasset。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());
    }

}
于 2012-03-01T10:59:41.640 に答える