package com.example.myapplication.db; import android.content.ContentValues; import android.content.Context; import android.content.res.Resources; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.Environment; import android.util.Log; import com.example.myapplication.R; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class MySQLiteOpenHelper extends SQLiteOpenHelper { private static final String DBname="uavshop.db"; //像控点数据库 private static final int version=1; private String tag="MySQLDBHelper"; private Context mycontext; String IMAGE = "file_data"; //blob字段名称 String TB_NAME = "kzdphoto";//含blob表名 public MySQLiteOpenHelper(Context context) { super(context, DBname, null, version); mycontext = context; Log.i(tag, "测试"); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub Log.i(tag, "uavshop数据库中kzdphoto创建前"); String sqlString="create table kzdphoto (id integer primary key autoincrement,file_name nvarchar(255),file_data blob,file_size int,file_type int,xkd_id int,upload_man nvarchar(32),upload_date nvarchar(64),crc int,desc nvarchar(255))"; db.execSQL(sqlString); Log.i(tag, "uavshop数据库中kzdphoto创建成功"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.i(tag, "更新数据库,暂时不需要使用"); } //--------------------------------------------------------------------------------------- public void Test() //测试函数 ,将cw_30.png图片文件插入数据表中 { try { //从资源中获取Bitmap对象 Resources res = mycontext.getResources(); Bitmap mybmp = BitmapFactory.decodeResource(res,R.drawable.cw_30); //转换为Byte数组 ByteArrayOutputStream os = new ByteArrayOutputStream(); mybmp.compress(Bitmap.CompressFormat.PNG, 100, os); byte[] img = os.toByteArray(); //插入数据库 insert(img); } catch (Exception ex) { ex.printStackTrace(); } } //------------------------------------------------------------------------------------------ public byte[] img(int id)//把图片转换成字节 { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Bitmap bitmap = ((BitmapDrawable) mycontext.getResources().getDrawable(id,null)).getBitmap(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } //------------------------------------------------------------------------------------------ //将图片一字节形式存储数据库读取操作 public long insert(byte[] img) { String desc = "文件大小为:"+Integer.toString(img.length)+"字节"; try { SQLiteDatabase db = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("file_name","PG001.jpg"); cv.put(IMAGE, img); cv.put("file_size",img.length); cv.put("file_type",1); cv.put("xkd_id",0); cv.put("upload_man","陈光荣"); cv.put("upload_date","2021-03-05 15:10:20"); cv.put("crc",25); cv.put("desc",desc); long result = db.insert(TB_NAME, null, cv); return result; } catch(Exception ex) { ex.printStackTrace(); return -1; } } //-----------------------读取blob字段转为图片----------------------------------- public Bitmap getBmp(int position) { SQLiteDatabase db = getReadableDatabase(); Cursor cursor = db.query(TB_NAME,null,null,null,null,null,null); cursor.moveToPosition(position); byte[] in = cursor.getBlob(cursor.getColumnIndex(IMAGE)); Bitmap bmpout = BitmapFactory.decodeByteArray(in, 0, in.length); return bmpout; } //------------------将Bitmap对象另存为图片文件------------------------------------------------------------------------ public void saveBitmap(String name, Bitmap bm) { Log.d("Save Bitmap", "Ready to save picture"); //指定我们想要存储文件的地址 String TargetPath = ""; File dataFilepath = Environment.getExternalStorageDirectory(); TargetPath = dataFilepath.getPath()+"/Pictures/"; Log.d("Save Bitmap", "Save Path=" + TargetPath); //判断指定文件夹的路径是否存在 if (!isExist(TargetPath)) { Log.d("Save Bitmap", "TargetPath isn't exist"); } else { //如果指定文件夹创建成功,那么我们则需要进行图片存储操作 File saveFile = new File(TargetPath, name); try { FileOutputStream saveImgOut = new FileOutputStream(saveFile); // compress - 压缩的意思 bm.compress(Bitmap.CompressFormat.PNG, 80, saveImgOut); //存储完成后需要清除相关的进程 saveImgOut.flush(); saveImgOut.close(); Log.d("Save Bitmap", "The picture is save to your phone!"); } catch (IOException ex) { ex.printStackTrace(); } } } //---------------------------判断文件夹是否存在------------------------------------------------------ static public boolean isExist(String path) { File file = new File(path); return file.exists(); } // ------------------------------------------------------------------------------------------------- }