Android 安装应用程序时是否在SD卡上复制文本文件?

Android 安装应用程序时是否在SD卡上复制文本文件?,android,android-sdcard,Android,Android Sdcard,我正在开发一款android游戏。我想在用户第一次安装游戏时将文本文件复制到外部SD卡。文本文件对于正确运行游戏非常重要 我该怎么做?我应该将文本文件放在eclipse源项目的何处,以便在构建apk文件时,我的文本文件也会捆绑在其中,并且当用户从该apk文件安装应用程序时,文本文件会复制到“SDcard\data”文件夹 我应该编写什么代码,在哪里编写,以便在安装时只执行一次 提前感谢对于此目标,最好的方法是制作SharedReference,或者必须将您的文件添加到android项目的“资产”

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

我该怎么做?我应该将文本文件放在eclipse源项目的何处,以便在构建apk文件时,我的文本文件也会捆绑在其中,并且当用户从该apk文件安装应用程序时,文本文件会复制到“SDcard\data”文件夹

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


提前感谢

对于此目标,最好的方法是制作SharedReference,或者必须将您的文件添加到android项目的“资产”目录中。

根据

有添加了广播意图的操作包,但正在安装的应用程序没有收到此信息。

看来使用SharedReferences是最简单的方法

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true);
p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();

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


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

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

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


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

这是我在首次安装应用程序时将文件复制到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);
        }
    }
公共类启动扩展活动{
/**
*--首次创建活动时调用。
* ==============================================================
**/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FirstRun();
}
私有void FirstRun(){
SharedReferences设置=this.getSharedReferences(“YourAppName”,0);
boolean firstrun=settings.getBoolean(“firstrun”,true);
if(firstrun){//检查是否运行了应用程序b4
SharedReferences.Editor e=settings.edit();
e、 putBoolean(“firstrun”,false);
e、 提交();
//如果没有,请运行以下方法:
SetDirectory();
意向主页=新意向(StartUp.this、MainActivity.class);
星触觉(家);
}else{//否则在此处启动应用程序:
意向主页=新意向(StartUp.this、MainActivity.class);
星触觉(家);
}
}
/**
*--检查SD卡是否已安装,并在其中创建一个目录
* ========================================================================
**/
私有void SetDirectory(){
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_-MOUNTED)){
extStorageDirectory=Environment.getExternalStorageDirectory().toString();
File txtDirectory=新文件(extStorageDirectory+“/yourAppName/txt/”;
//创造
//a
//文件
//反对
//为了
//
//母公司
//目录
txtDirectory.mkdirs();//让对象生成目录
//结构,如果需要。
CopyAssets();//然后运行该方法复制文件。
}else if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)){
AlertsAndDialogs.sdCardMissing(this);//或使用您自己的方法,例如:Toast
}
}
/**
*--将文件从资产文件夹复制到SD卡
* ===========================================================
**/
私人资产(){
AssetManager AssetManager=getAssets();
String[]files=null;
试一试{
files=assetManager.list(“”);
}捕获(IOE异常){
Log.e(“tag”,e.getMessage());
}
对于(int i=0;i
我认为假设用户将拥有SD卡是不公平的。不需要运行Android。好吧,假设我已经使用getExternalStorageState()检查了SD卡。它是存在的。你不能在安装时真正执行代码。你的第一次机会是你的应用程序第一次启动。@Tim我知道我们不能在安装时运行代码。实际上,在安装时我指的是在第一次运行时。我在sd卡中得到文件NotFoundException。它不是在sd卡中创建文件。请发布你的代码用法和日志。我在我的应用程序中使用它,没有问题-你只需要添加你的信息。嗨,我成功了。犯了一个愚蠢的错误。非常感谢,伙计…:)