3

私はアンドロイドゲームに取り組んでいます。ユーザーが初めてゲームをインストールするときに、テキストファイルを外部SDカードにコピーしたい。テキストファイルは、ゲームを正しく実行するために重要です。

どうやってやるの?テキストファイルをEclipseソースプロジェクトのどこに配置すれば、apkファイルをビルドするときにテキストファイルもバンドルされ、使用時にそのapkファイルからアプリケーションをインストールすると、テキストファイルが「SDcard\data」フォルダーにコピーされます。 。?

インストール時に1回だけ実行されるように、どのコードをどこに書き込む必要がありますか。

前もって感謝します

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

リンクごとに

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

ファイルをアセットフォルダーに置きます。次に、思いついたロジックを使用して、アプリの起動時に、それがアプリの最初の実行であるかどうかを判断します。

そうである場合は、アクティビティからgetAssets()を使用してアセットファイルにアクセスし、必要に応じてコピーすることができます。

于 2012-06-20T14:33:03.980 に答える
0

このターゲットの最善の方法は、SharedPreferencesを作成するか、Androidプロジェクトの「assets」ディレクトリにファイルを追加する必要があります。

于 2012-06-20T14:31:24.943 に答える
0

SDカード上のファイルはユーザーが誤って削除する可能性があるため、共有設定などの独立したものを使用してこれが最初かどうかを判断するのではなく、その存在を直接確認する(場合によっては内容を確認する)必要があります。アクティビティの実行。

アプリをアップグレードする可能性があるため、ファイルにバージョン番号を入れて確認する必要があります。

ファイルがパワーユーザーに手動で編集させたいものである場合(エキスパートオプションを変更するため)、アップグレード時に処理するのが少し難しい状況になる可能性があります。

于 2012-06-20T14:51:25.593 に答える