0

私のアプリでは、押すとアプリのrawフォルダーに保存されているファイルをsdcard / Android/dataにコピーするボタンが必要です...すでに存在する既存のファイルを上書きします。

これが私がこれまでに持っているものです。例として、rawフォルダー内のファイルを呼び出しますbrawler.dat

私は誰かにコード全体を書くように頼んではいないが、それは確かにボーナスだろう。

私は主に正しい方向に私を向ける誰かが必要です。

URLなどに移動するボタンを作成することはできますが、次のレベルの準備ができていると感じています。

main.xml

 rLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Overwrite File" />

FreelineActivity.java

     package my.freeline.conquest;

import android.app.Activity;
import android.os.Bundle;

public class FreelineActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
4

2 に答える 2

0

次の方法で生のリソース入力ストリームを取得します。

// in your activity in `onClick` event of the button:
InputStream is = getResources().openRawResource(R.raw.yourResourceName);

次に、それをバッファーに読み取り、ファイル出力ストリームに書き込みます。

OutputStream os = new FileOutputStream("real/path/name"); // you'll need WRITE_EXTERNAL_STORAGE permission for writing in external storage
byte[] buffer = new byte[1024];
int read = 0;
while ((read = is.read(buffer, 0, buffer.length)) > 0) {
  os.write(buffer, 0, size);
}
is.close();
os.close();
于 2012-03-31T20:49:27.757 に答える
0

次の簡単な呼び出しを行うことができますcopyRawFile()。ストレージの詳細については、http://developer.android.com/guide/topics/data/data-storage.htmlを参照してください

    private void copyRawFile() {


                InputStream in = null;
                OutputStream out = null;
    String filename="myFile"; //sd card file name           
    try {
//Provide the id of raw file to the openRawResource() method
                  in = getResources().openRawResource(R.raw.brawler);
                  out = new FileOutputStream("/sdcard/" + filename);
                  copyFile(in, out);
                  in.close();
                  in = null;
                  out.flush();
                  out.close();
                  out = null;
                } catch(Exception e) {
                    Log.e("tag", e.getMessage());
                }       

        }
        private void copyFile(InputStream in, OutputStream out) throws IOException {
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
              out.write(buffer, 0, read);
            }
        }
于 2012-03-31T20:55:05.460 に答える