2010-10-03 36 views
8

Tôi đang cố gắng tạo thư mục và một số thư mục con bên trong nó trên Thẻ SD ... Sau đó tôi muốn chuyển các tệp mà tôi đã lưu trữ trong/res/raw vào thư mục đó ... Thêm vào đó, tôi chỉ muốn xảy ra một lần, lần đầu tiên chương trình được chạy. Tôi nhận ra rằng điều này là vô cùng cởi mở, và rằng tôi đang yêu cầu rất nhiều ... nhưng bất kỳ sự giúp đỡ nào cũng sẽ được đánh giá cao.Android: Cách tạo thư mục trên Thẻ SD và sao chép tệp từ/res/raw sang nó?

Trả lời

9

này sẽ sao chép tất cả các file trong "clipart" thư mục con của thư mục nội dung .apk vào "clipart" thư mục con của thư mục của ứng dụng của bạn trên thẻ SD:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
    String basepath = extStorageDirectory + "/name of your app folder on the SD card"; 
//... 

// in onCreate 
File clipartdir = new File(basepath + "/clipart/"); 
     if (!clipartdir.exists()) { 
      clipartdir.mkdirs(); 
      copyClipart();  
     } 

private void copyClipart() { 
     AssetManager assetManager = getResources().getAssets(); 
     String[] files = null; 
     try { 
      files = assetManager.list("clipart"); 
     } catch (Exception e) { 
      Log.e("read clipart ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
     for(int i=0; i<files.length; i++) { 
      InputStream in = null; 
      OutputStream out = null; 
      try { 
       in = assetManager.open("clipart/" + files[i]); 
       out = new FileOutputStream(basepath + "/clipart/" + files[i]); 
       copyFile(in, out); 
       in.close(); 
       in = null; 
       out.flush(); 
       out.close(); 
       out = null; 
      } catch(Exception e) { 
       Log.e("copy clipart ERROR", e.toString()); 
       e.printStackTrace(); 
      }  
     } 
    } 
    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

mã này cần phải đảm bảo nó chỉ xảy ra một lần, khi cài đặt và sau đó xóa tệp khỏi APK? – joon

+0

@joon: Bạn có thể sử dụng [link] (http://developer.android.com/reference/android/content/SharedPreferences.html) để lưu một giá trị boolean để đảm bảo nó chỉ xảy ra một lần. Nhân tiện, bạn không thể xóa tệp khỏi apk. @ Xem thêm: [link] (http://developer.android.com/guide/topics/resources/providing-resources.html) – Justin

0

tôi gặp phải vấn đề tương tự khi sử dụng mkdirs(), tuy nhiên vì cách chạy lệnh:

mkdir một/hai

thất bại trên Linux, thì subse phương pháp http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs() quently thất bại quá. Tôi đoán điều này có nghĩa là không có cách nào để sử dụng mkdir trên Android? Công việc của tôi (có thể khá khó khăn) là tạo từng thư mục cần thiết riêng biệt:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
new File(extStorageDirectory + "/one/").mkdirs(); 
new File(extStorageDirectory + "/one/two/).mkdirs(); 
+0

[File.mkDirs() ] (http://developer.android.com/reference/java/io/File.html#mkdirs%28%29) hoạt động. Tôi đã dùng thử trên Android 2.1 (API cấp 7) – Mudassir

Các vấn đề liên quan