目前正在一个项目中工作,我需要将在线视频 url 和播放视频的总时间存储在本地存储(内部和外部)中。但我不知道如何实现这一目标。我总共有 5 个视频,我需要维护一个文件来存储所有值。
谁能告诉我如何实现这一目标?我提到了Android 的保存文件培训,但无法清楚地了解。
目前正在一个项目中工作,我需要将在线视频 url 和播放视频的总时间存储在本地存储(内部和外部)中。但我不知道如何实现这一目标。我总共有 5 个视频,我需要维护一个文件来存储所有值。
谁能告诉我如何实现这一目标?我提到了Android 的保存文件培训,但无法清楚地了解。
最后我解决了我的问题,我将文件写入外部存储并将它们存储为文本文件:
FileOutputStream fos;
try {
fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
}
这真的是一件简单的事情,它帮助我将我的文件作为文本文件编写和查看。希望这可以帮助某人:-)
我猜你可以使用设备数据库(SQLite 数据库)来存储信息
如何使用、添加和检索数据看看这个示例
http://www.vogella.com/articles/AndroidSQLite/article.html
如果您不想存储信息,只需将该信息写入文件并将该文件保存在设备中。
/**
* For writing the data into the file.
* @param context
* @param filename
* @param data
*/
public static void writeData(Context context, String filename, String data) {
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput(filename,
Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* For reading file from the device.
*
* @param filename
* @param context
* @return
*/
private String getData(String filename, Context context) {
StringBuffer data = new StringBuffer();
try {
FileInputStream openFileInput = context.openFileInput(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(
openFileInput));
String _text_data;
try {
while ((_text_data = reader.readLine()) != null) {
data.append(_text_data);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return data.toString();
}