Android:如何在首次运行时将文件从 Assets 加载到 SD?

标签 android file sd-card assets

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

最佳答案

这是我使用的方法。将其作为您应用程序的主要 Activity ,并使用它来启动您真正的应用程序 Activity 。

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

关于Android:如何在首次运行时将文件从 Assets 加载到 SD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11128260/

相关文章:

bash - 如何按编号的百分比拆分文件。线?

linux - 每个镜像的 Raspberry Pi 安装程序

Android - 存储从网络下载的图像

android - 如何在 ListView 中显示空列表?

android - 我想播放 ListView 中选定的 mp3 文件

linux - 我如何检测何时有人在 Linux 中打开 pty(伪终端)的从属端?

node.js - nodejs - 如何更改文件的创建时间

java - 能否绕过 HEAP 从 HTTP 直接将字节流写入 SDCard?

android - 如何在PhoneGap中添加滚动条

android - 使用任何IP和任何端口号android监听数据报套接字