2012-06-20 4 views

답변

2

다음은 내가 사용하는 방법입니다. 이것을 응용 프로그램의 주요 활동으로 사용하고 실제 응용 프로그램 활동을 시작하는 데 사용하십시오.

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, YourMainActivity.class); 
      startActivity(home); 

     } else { // Otherwise start the application here: 

      Intent home = new Intent(StartUp.this, YourMainActivity.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/txtDirectory/");//Example name for txt files 
      // 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); 
     } 
    } 
관련 문제