我一直在研究如何创建一个文件,然后在内部存储中写入它几天,并且无法找到任何可行的或我能理解的内容。我之前从未做过这样的事情,所以我很遗憾。
根据android开发者网站页面,这段代码应该创建一个文件夹,如果它不存在,但是当我调用它时,它会给我一个错误。如果这意味着什么,该类扩展了SurfaceView。
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
System.exit(1);
}
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
createFile();
}
public void createFile() {
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos;
try {
fos = getContext().openFileOutput(FILENAME,Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
我想最终能做的就是在内部存储中创建一个文件,将整数(我的玩家在游戏中得分)保存到该文件中,然后才能加载和保存此
谢谢。
答案 0 :(得分:0)
如果您想实现持久性键值对,我建议Android's SharedPreferences。使用SharedPreferences
非常简单:
final SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); //MY_PREFS_NAME could be something useful, like username, so your data can be easily separated.
final static SCORE = "score"; //This variable simply holds the string "score", so we can tell the SharedPreferences which data we'd like.
int getScore() {
return prefs.getInt(SCORE, 0); // second argument is default value if there is no value for key.
}
void setScore(int score) {
Editor editor = prefs.edit();
editor.putInt(SCORE, score);
editor.commit();
}
这些设置将在您的应用程序的多次运行中持续存在。
答案 1 :(得分:0)
这是我在本地编写和阅读照片文件的方式。可以应用类似的过程来处理不同类型的文件。只需使用不同的文件扩展名和不同的方法返回数据类型。
// Creates an ImageFile
private File createNewPhotoFile() throws IOException {
String imageFileName = "JPEG";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
image.createNewFile();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
FileOutputStream fo = new FileOutputStream(image);
fo.write(stream.toByteArray());
fo.close();
return image;
}
// Retreave and convert from file to Bitmap
private Bitmap retreaveImageFromFile(ImageView target, File fileForPath) {
// Get the dimensions of the View
int targetW = target.getWidth();
int targetH = target.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileForPath.getAbsolutePath(), bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = 4; //Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(newPhotoFile.getAbsolutePath(), bmOptions);
return bitmap;
}