gpt4 book ai didi

android - 在安装应用程序时复制 SD 卡上的文本文件?

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:35 25 4
gpt4 key购买 nike

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

我该怎么做?我应该将文本文件放在 eclipse 源项目中的什么位置,这样当我构建 apk 文件时,我的文本文件也被 bundle 在其中,当用户从该 apk 文件安装应用程序时,文本文件被复制到“SDcard\data”文件夹.?

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

提前致谢

最佳答案

这是我在第一次安装应用程序时将文件复制到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);
}
}

关于android - 在安装应用程序时复制 SD 卡上的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com