当前位置: 移动技术网 > IT编程>移动开发>Android > Android数据库SD卡创建和图片存取操作

Android数据库SD卡创建和图片存取操作

2019年07月24日  | 移动技术网IT编程  | 我要评论

痞子英雄陈意涵,维尼夫妇ep53,1953年1月8日人民日报

android数据库中的创建,图片的存、取操作如下:

数据库类:

import android.content.context; 
import android.database.sqlite.sqlitedatabase; 
import android.database.sqlite.sqliteopenhelper; 
import android.util.log; 
 
/** 
 * 此类继承了sqliteopenhelper抽象类,是一个辅助器类,需要 一个构造函数和重写两个方法。 
 * 
 */ 
public class mysqliteopenhelper extends sqliteopenhelper { 
  public static final string database_name = "text.db"; // 数据库名 
  public static final int version = 1; // 版本号 
  public static final string table_name = "text"; // 表名 
  public static final string id = "id"; 
  public static final string image = "image"; 
 
  public mysqliteopenhelper(context context) { 
    super(context, database_name, null, version); 
  } 
 
  /** 
   * 在数据库第一次生成的时候会调用这个方法,同时我们在这个方法里边生成数据库表 
   */ 
  @override 
  public void oncreate(sqlitedatabase db) { 
    // 创建数据表的操作 
    string strsql = "create table " + table_name + "(" + id 
        + " integer primary key autoincrement," + image + " blob not null );"; 
 
    db.execsql(strsql); 
  } 
 
  /** 
   * 更新或者升级数据库的时候会自动调用这个方法,一般我们会在这个方法中 删除数据表,然后再创建新的数据表操作。 
   */ 
  @override 
  public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { 
    log.e("andydemo", "onupgrade"); 
  } 
} 

activity:

private button btn_newtable, btn_addone, get_image; 
private textview tv; 
private imageview showimage; 
private mysqliteopenhelper myopenhelper; 
private sqlitedatabase sqlitedb; 
private file path = new file("sdcard/text"); // 数据库文件目录 
private file f = new file("sdcard/text/text.db"); // 数据库文件 
 
@override 
public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
 
  init(); 
 
  // 实例化默认数据库辅助操作对象 
  myopenhelper = new mysqliteopenhelper(this); 
 
  // sd卡中创建数据库文件 
  if (!path.exists()) { // 判断目录是否存在 
    path.mkdirs(); // 创建目录 
  } 
  if (!f.exists()) { // 判断文件是否存在 
    try { 
      f.createnewfile(); // 创建文件 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 
 
/** 
 * 初始化ui界面 
 */ 
private void init() { 
  tv = (textview) findviewbyid(r.id.tv_result); 
  btn_newtable = (button) findviewbyid(r.id.newtable); 
  btn_addone = (button) findviewbyid(r.id.addone); 
  get_image = (button) findviewbyid(r.id.getimage); 
  showimage = (imageview) findviewbyid(r.id.showimage); 
  btn_newtable.setonclicklistener(new clickevent()); 
  btn_addone.setonclicklistener(new clickevent()); 
  get_image.setonclicklistener(new clickevent()); 
 
} 
 
class clickevent implements onclicklistener { 
  @override 
  public void onclick(view v) { 
    try { 
      // sd卡中创建数据库,实例化sqlitedb的操作如下 
      sqlitedb = sqlitedatabase.openorcreatedatabase(f, null); 
      if (v == btn_newtable) { // 1.新建数据表 
        string table_name = "text"; 
        string id = "id"; 
        string image = "image"; 
        string str_sql2 = "create table " + table_name + "(" + id 
            + " integer primary key autoincrement," + image 
            + " blob not null );"; 
        sqlitedb.execsql(str_sql2); 
        tv.settext("新建数据表成功!"); 
 
      } else if (v == btn_addone) { // 2.插入一条记录 
        contentvalues values = new contentvalues(); 
        values.put( 
            mysqliteopenhelper.image, 
            drawablechange(getresources().getdrawable( 
                r.drawable.ic_launcher))); 
        sqlitedb.insert(mysqliteopenhelper.table_name, null, values); 
        tv.settext("添加新数据成功!"); 
      } else if (v == get_image) { 
        cursor c = sqlitedb.rawquery("select * from text", null); 
        c.movetolast(); 
 
        if (c.islast()) { 
          byte[] blob = c.getblob(c 
              .getcolumnindex(mysqliteopenhelper.image)); 
          bitmap bmp = bitmapfactory.decodebytearray(blob, 0, 
              blob.length); 
          showimage.setimagebitmap(bmp); 
        } 
        c.close(); 
      } 
    } catch (exception e) { 
      tv.settext("操作失败"); 
    } finally { 
      sqlitedb.close(); 
    } 
  } 
} 
 
/** 
 * drawable转化成字节数组 
 * 
 * @param drawable 
 * @return 
 */ 
private byte[] drawablechange(drawable drawable) { 
 
  bitmap bm = ((bitmapdrawable) drawable).getbitmap(); 
  bytearrayoutputstream baos = new bytearrayoutputstream(); 
  bm.compress(bitmap.compressformat.png, 100, baos); 
  byte[] date = baos.tobytearray(); 
  return date; 
} 

新建一张表,插入一张图片,效果图如下:

查看表中的数据如下:

取出图片:

ok!!搞定! 最后别忘了加权限:

<uses-permission android:name="android.permission.write_external_storage" /> 
<uses-permission android:name="android.permission.mount_unmount_filesystems"/> 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网