- 从AndroidStudio assets目录复制文件到sd卡目录:(后两个参数表示sd卡路径及文件名称)
{
boolean result = false;
try {
if (!(new File(filepath +"/"+ fileName)).exists()) { //sd卡文件不存在
File f = new File(filepath);
if (!f.exists()) {
f.mkdir();
}
try {
InputStream is = context.getAssets().open(fileName); //assets资源文件
OutputStream os = new FileOutputStream(filepath +"/"+ fileName);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
} else {
result = true;
}
} catch (Exception e) {
Log.d("fcj",e.toString());
}
return result;
}
- 处理sd卡SQL文件,需要通过继承SQLiteOpenHelper类,重写其构造方法。因为Android通过SQLiteOpenHelper创建数据库时,默认是将文件保存在'/data/data/应用程序名/databases'目录下,默认的SQLiteOpenHelper的构造方法调用了Context的openOrCreateDatabase方法,所以我们也需要继承Context类,重写openOrCreateDatabase方法,指定sd卡数据库路径。
- 继承SQLiteOpenHelper :
public class SQLiteHelper extends SQLiteOpenHelper{public static String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/1SQL";
public static String DB_NAME = "Identity.sqlite";
public SQLiteHelper(Context context) {super(new SQLiteContext(context, getDirPath ()), DB_NAME, null, 1);}private static String getDirPath(){if(checkDataBase()) {return DATABASE_PATH;}return null;}public static boolean checkDataBase() { //查询该数据库文件是否存在SQLiteDatabase checkDB = null;try {String myPath = DATABASE_PATH +"/"+ DB_NAME;checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);} catch (SQLiteException e) {}if (checkDB != null) {checkDB.close();}return checkDB != null ? true : false;}@Overridepublic void onOpen(SQLiteDatabase db) {super.onOpen(db);}
@Overridepublic void onCreate(SQLiteDatabase db) {
}
@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}}
- 继承ContextWrapper:
{
private String mDirPath; //sd卡文件路径,不包括文件名称
public SQLiteContext(Context base,String dirPath){
super(base);
this.mDirPath=dirPath;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
return super.openOrCreateDatabase( getDatabasePath (name).getAbsolutePath(), mode, factory, errorHandler);
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
return super.openOrCreateDatabase( getDatabasePath (name).getAbsolutePath(),mode, factory);
}
@Override
public File getDatabasePath (String name) { //name:文件名称
File result = new File( mDirPath +File.separator +name);
if (!result.getParentFile().exists()){
result.getParentFile().mkdirs();
}
return result;
}
}
- 查询数据,将数据转成图片:
SQLiteHelper helper = new SQLiteHelper(this);
SQLiteDatabase db = helper.getReadableDatabase();
String[] columns = {"id", "photo", "name", "gender", "place"};
//你要的数据
String[] selectionArgs = {"VBN97XnLE"};
//选择的目标值
//查询identify表中,id=VBN97XnLE的数据
Cursor cursor = db.query("identity", columns, "id=?", selectionArgs, null, null, null);
while (cursor.moveToNext()) {
int nameColumnIndex = cursor.getColumnIndex("photo");
//数据“photo”所在的列数
byte[] strValue = cursor.getBlob(nameColumnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(strValue, 0, strValue.length);
bitmapView.setImageBitmap(bitmap);
}
- 数据库数据