【问题标题】:Android: How to Load a File From Assets to SD at First Run?Android:如何在首次运行时将文件从资产加载到 SD?
【发布时间】:2012-06-20 21:12:08
【问题描述】:

如何将文件(SQLlite、文本等)从 Assets 文件夹加载到 SD 卡上,并且仅在应用程序首次运行时加载?

【问题讨论】:

    标签: android file sd-card assets


    【解决方案1】:

    这是我使用的方法。将此作为您应用程序的主要活动,并使用它来启动您的实际应用程序活动。

    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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 2020-02-23
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多