2012-06-20 65 views
3

我正在開發一款安卓遊戲。我想在用戶第一次安裝遊戲時將文本文件複製到外部SD卡。文本文件對於正確運行遊戲非常重要。應用程序安裝時,在SD卡上覆制文本文件?

我該怎麼做?我應該在eclipse源項目中放置文本文件,這樣當我生成apk文件時,我的文本文件也會被捆綁到它中,並且當用戶從該apk文件安裝應用程序時,文本文件將被複制到「SDcard \ data」文件夾。?

我應該寫什麼代碼和哪裏,以便在安裝時只執行一次。

在此先感謝

+1

我不認爲這是公平的假設用戶將有一個SD卡在所有。不需要運行Android。 – DynamiteReed

+0

好吧,假設我使用getExternalStorageState()檢查了SD卡。它存在。 – Pargat

+0

您在安裝時無法真正執行代碼。您的第一次機會是您的應用程序第一次啓動。 – FoamyGuy

回答

6

這是我使用的文件複製到SD卡首次安裝應用程序時的方法:

public class StartUp extends Activity { 

    /** 
    * -- Called when the activity is first created. 
    * ============================================================== 
    **/ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FirstRun(); 
    } 

    private void FirstRun() { 
     SharedPreferences settings = this.getSharedPreferences("YourAppName", 0); 
     boolean firstrun = settings.getBoolean("firstrun", true); 
     if (firstrun) { // Checks to see if we've ran the application b4 
      SharedPreferences.Editor e = settings.edit(); 
      e.putBoolean("firstrun", false); 
      e.commit(); 
      // If not, run these methods: 
      SetDirectory(); 
      Intent home = new Intent(StartUp.this, MainActivity.class); 
      startActivity(home); 

     } else { // Otherwise start the application here: 

      Intent home = new Intent(StartUp.this, MainActivity.class); 
      startActivity(home); 
     } 
    } 

/** 
    * -- Check to see if the sdCard is mounted and create a directory w/in it 
    * ======================================================================== 
    **/ 
    private void SetDirectory() { 
     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { 

      extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 

      File txtDirectory = new File(extStorageDirectory + "/yourAppName/txt/"); 
      // Create 
      // a 
      // File 
      // object 
      // for 
      // the 
      // parent 
      // directory 
      txtDirectory.mkdirs();// Have the object build the directory 
      // structure, if needed. 
      CopyAssets(); // Then run the method to copy the file. 

     } else if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) { 

      AlertsAndDialogs.sdCardMissing(this);//Or use your own method ie: Toast 
     } 

    } 

    /** 
    * -- Copy the file from the assets folder to the sdCard 
    * =========================================================== 
    **/ 
    private void CopyAssets() { 
     AssetManager assetManager = getAssets(); 
     String[] files = null; 
     try { 
      files = assetManager.list(""); 
     } catch (IOException e) { 
      Log.e("tag", e.getMessage()); 
     } 
     for (int i = 0; i < files.length; i++) { 
      InputStream in = null; 
      OutputStream out = null; 
      try { 
       in = assetManager.open(files[i]); 
       out = new FileOutputStream(extStorageDirectory + "/yourAppName/txt/" + files[i]); 
       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

我得到FileNotFoundException在SD卡中的文件。它不是在SD卡中創建文件。 – Pargat

+0

Plz發佈您的代碼使用情況和LogCat。我在我的應用程序不使用這個問題 - 沒有問題 - 所有你需要的是添加你的信息 – CelticParser

+0

嗨,我得到它的工作..做一個愚蠢的錯誤。非常感謝好友...... :) – Pargat

0

爲了實現這一目標的最好辦法是讓SharedPreferences或文件必須在Android項目「資產」目錄添加。

0

link

還有就是ACTION_PACKAGE_ADDED廣播意圖,but the application being installed doesn't receive this.

,所以它看起來使用SharedPreferences是最簡單的方法...

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this); 
boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true); 
p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit(); 
0

把文件中的資產的文件夾。然後,使用您提出的任何邏輯,當您的應用程序啓動時,確定它是否是該應用程序的第一次運行。

如果是可以使用來自Activity的getAssets()來訪問資產文件並將其複製到任何需要的位置。

0

由於SD卡上的文件可能會被用戶意外刪除,因此您應該直接檢查它的存在(並可能驗證內容),而不是嘗試使用獨立的內容(如共享首選項)來判斷這是該活動的第一次運行。

對於潛在的應用程序升級,您應該在文件中添加一個版本號,然後檢查。

如果該文件是您想讓高級用戶手動編輯(更改專家選項)的文件,那麼您可能會遇到一些棘手的情況來處理升級。

相關問題