2012-03-31 1 views
0

내 앱에서 버튼을 누르면 내 응용 프로그램 원시 폴더에 저장된 파일이 sdcard/Android/data ...에 이미 복사되어있는 기존 파일을 덮어 쓰게됩니다.버튼을 sdcard에 파일로 쓰는 방법은 무엇입니까?

여기까지 제가 지금까지 있습니다. 예를 들어, 원시 폴더에있는 파일의 이름은 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); 
    } 
} 

답변

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