3

我正在开发一款安卓游戏。我想在用户第一次安装游戏时将文本文件复制到外部 SD 卡。文本文件对于正确运行游戏很重要。

我怎样才能做到这一点?我应该将文本文件放在 Eclipse 源项目中的哪个位置,以便在构建 apk 文件时,我的文本文件也被捆绑在其中,当用户从该 apk 文件安装应用程序时,文本文件被复制到“SDcard\data”文件夹.?

我应该编写什么代码以及在哪里编写,以便在安装时只执行一次。

提前致谢

4

5 回答 5

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);
        }
    }
于 2012-06-20T20:58:57.757 回答
0

由于 sdcard 上的文件可能会被用户意外删除,因此您可能应该直接检查它的存在(并可能验证内容),而不是尝试使用独立的东西(例如共享首选项)来判断这是否是第一个活动的运行。

出于潜在应用程序升级的目的,您可能应该在文件中输入一个版本号,并检查它。

如果您想让高级用户手动编辑该文件(以更改专家选项),那么您在升级时可能会遇到一些具有挑战性的情况。

于 2012-06-20T14:51:25.593 回答
0

对于这个目标,最好的方法是制作 SharedPreferences 或者您的文件必须添加到 android 项目的“assets”目录中。

于 2012-06-20T14:31:24.943 回答
0

根据链接

有 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();
于 2012-06-20T14:32:35.257 回答
0

将文件放在资产文件夹中。然后,使用您想出的任何逻辑,在您的应用启动时确定它是否是应用的第一次运行。

如果是,您可以使用 Activity 中的 getAssets() 来访问资产文件并将其复制到任何需要的地方。

于 2012-06-20T14:33:03.980 回答