2012-05-02 18 views
0

下記のコードを確認してください。 リソースファイルをAndroid端末の外部ストレージにコピーできません。 私はコメントしたコードを使用してコピーできますが、元のコードの問題を知りたいのですが。Androidの外部ストレージにファイルを書き込めません

public class ExternalData extends Activity implements OnItemSelectedListener, 
    OnClickListener { 

TextView canRead = null; 
TextView canWrite = null; 
String state = null; 
boolean canR, canW; 

EditText userEnteredFileName; 
Button bConfirm, bSaveAs; 

Spinner spinner = null; 
String[] stuff = { "Music", "Pictures", "Downloads" }; 
File path = null; 
File file = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.externaldata); 

    canRead = (TextView) findViewById(R.id.tvCanRead); 
    canWrite = (TextView) findViewById(R.id.tvCanWrite); 

    userEnteredFileName = (EditText) findViewById(R.id.etUserEnteredFileName); 
    bConfirm = (Button) findViewById(R.id.bConfirm); 
    bSaveAs = (Button) findViewById(R.id.bSaveAs); 

    bConfirm.setOnClickListener(this); 
    bSaveAs.setOnClickListener(this); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, stuff); 
    spinner = (Spinner) findViewById(R.id.spinner1); 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(this); 

} 

@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 

    int position = spinner.getSelectedItemPosition(); 
    switch (position) { 
    case 0: 
     path = Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); 
     break; 
    case 1: 
     path = Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
     break; 
    case 2: 
     path = Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
     break; 
    } 
} 

public void checkState() { 

    state = Environment.getExternalStorageState(); 
    if (state.equals(Environment.MEDIA_MOUNTED)) { 
     canRead.setText("Can Read: true"); 
     canWrite.setText("Can Write: true"); 
     canR = canW = true; 
    } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { 
     canRead.setText("Can Read: true"); 
     canWrite.setText("Can Write: false"); 
     canR = true; 
     canW = false; 
    } else { 
     canRead.setText("Can Read: false"); 
     canWrite.setText("Can Write: false"); 
     canR = canW = false; 
    } 
} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch (arg0.getId()) { 
    case R.id.bConfirm: 
     bSaveAs.setVisibility(View.VISIBLE); 
     break; 
    case R.id.bSaveAs: 
     checkState(); 
     if(canR == canW == true){ 

      file = new File (path, userEnteredFileName.getText().toString() + "/"); 


      try { 
       if(!file.exists()){ 
        boolean success = file.mkdirs(); 
        if(success){ 
         Toast.makeText(ExternalData.this, "File created", Toast.LENGTH_LONG).show(); 
        }else{ 
         Toast.makeText(ExternalData.this, "Not able to create file", Toast.LENGTH_LONG).show(); 
        } 
       } 
       InputStream fis = getResources().openRawResource(R.drawable.darthvader); 
       /* this works....but wats wrong with the original code 
       file = new File(file, "darthVader.png"); 
       if(!file.exists()){ 
        file.createNewFile(); 
       } 
       */ 
       FileOutputStream fos = new FileOutputStream(file); 
       byte[] data = new byte[fis.available()]; 
       fis.read(data); 
       fos.write(data); 
       fis.close(); 
       fos.close(); 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(ExternalData.this, "FilNOT", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(ExternalData.this, "IO", Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 
     } 
     break; 
    } 
} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 

} 

答えて

0

は忘れていないSDカード内のファイルを作成する方法についてthis例(外部記憶装置)

 InputStream input; 
     try { 
      URL url = new URL (strURL); 
      input = url.openStream(); 
      byte[] buffer = new byte[1500]; 
      OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png"); 


       int bytesRead = 0; 
       while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) 
       { 
        output.write(buffer, 0, bytesRead); 
       } 

      finally 
      { 
       output.close(); 
       buffer=null; 
      } 

を見ますmanifeastファイルにpermissonを与える

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
1

マニフェストで許可をリクエストしましたか?

<は、Androidの許可使用しています:名= "android.permission.WRITE_EXTERNAL_STORAGE"/>

+0

android.permission.WRITE_EXTERNAL_STORAGE 湯p –

+0

@rahuls誰かがあなたを助けてくれたら、upvote – MAC

関連する問題