app开发存储功能实现

随着智能手机的普及,各类应用程序如雨后春笋般涌现,应用程序提供的各种服务让我们的生活变得更加便捷。而这些应用程序需要存储数据,以保存用户的个人信息和其它数据,这些数据在如今的移动互联网时代非常重要。本文将介绍如何在app开发中添加存储功能。

存储是指利用设备上的存储介质(如手机内置存储空间,SD卡等)来保存应用程序中的数据。在手机开发中,常用的有共享首选项、内置存储、外置存储、SQLite数据库、网络等多种存储方式。以下分别对这些存储方式进行介绍。

1. 共享首选项

首选项是指应用程序的一些配置参数,比如用户名、密码等信息,通常使用键值对的方式存储在设备上。应用程序可以使用android.content.SharedPreferences类来存储和访问这些首选项。

SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();

editor.putString("username", "yang");

editor.putString("password", "123456");

editor.apply();

SharedPreferences preferences = getSharedPreferences("user", MODE_PRIVATE);

String username = preferences.getString("username", "");

String password = preferences.getString("password", "");

2. 内置存储

在Android应用程序中,内置存储是指应用程序安装后访问的私有内存,可以考虑将一些本地文件保存在这里。在内置存储中保存文件的路径通常是data/data/包名/files/子目录。

FileOutputStream outputStream;

try {

outputStream = openFileOutput("myfile", Context.MODE_PRIVATE);

outputStream.write("hello world".getBytes());

outputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

FileInputStream inputStream;

try {

inputStream = openFileInput("myfile");

byte[] bytes = new byte[inputStream.available()];

inputStream.read(bytes);

String content = new String(bytes);

inputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

3. 外置存储

外置存储是指SD卡等储存设备。由于外置存储设备通常是共享的,因此需要请求用户授权,才能在应用程序中访问和写入数据。

//检查授权

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

} else {

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

File directory = Environment.getExternalStorageDirectory();

File file = new File(directory, "test.txt");

FileOutputStream outputStream;

try {

outputStream = new FileOutputStream(file);

outputStream.write("hello world".getBytes());

outputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

FileInputStream inputStream;

try {

inputStream = new FileInputStream(file);

byte[] bytes = new byte[inputStream.available()];

inputStream.read(bytes);

String content = new String(bytes);

inputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

4. SQLite数据库

SQLite是一种轻型关系型数据库管理系统,被广泛应用于安卓应用程序中。应用程序可以通过使用android.database.sqlite包中的类来访问和操作SQLite数据库。

public class MyDatabaseHelper extends SQLiteOpenHelper {

public static final String CREATE_BOOK = "create table Book ("

+ "id integer primary key autoincrement, "

+ "author text, "

+ "price real, "

+ "pages integer, "

+ "name text)";

private Context mContext;

public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {

super(context, name, factory, version);

mContext = context;

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_BOOK);

Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

MyDatabaseHelper dbHelper = new MyDatabaseHelper(MainActivity.this, "BookStore.db", null, 1);

SQLiteDatabase db = dbHelper.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("author", "Yang");

values.put("price", 16.96);

values.put("pages", 454);

values.put("name", "The way of life");

db.insert("Book", null, values);

Cursor cursor = db.query("Book", null, null, null, null, null, null);

if (cursor.moveToFirst()) {

do {

String author = cursor.getString(cursor.getColumnIndex("author"));

double price = cursor.getDouble(cursor.getColumnIndex("price"));

int pages = cursor.getInt(cursor.getColumnIndex("pages"));

String name = cursor.getString(cursor.getColumnIndex("name"));

} while (cursor.moveToNext());

}

cursor.close();

5. 网络

应用程序还可以通过网络存储和读取数据。在应用程序中直接进行网络操作需要考虑到网络的延迟和带宽问题,因此建议将网络存储方式结合其它存储方式一起使用。

总结一下,当我们在编写应用程序时,需要考虑的存储方式有很多,应选择适合应用程序实现的存储方式。而对于不同的存储方式,所涉及的类和方法也不尽相同。通过使用android API,可以方便地实现上述存储方式。

川公网安备 51019002001185号