SQLite的使用------图片存储

在上篇有说到SQLite的文本存储,文本的增删改查,在本篇中主要说SQLite对单张图片的存储。

简单介绍:

SQLiteOpenHelper是一个抽象类。

SQLiteOpenHelper中有两个抽象方法,分别是onCreate()和onUpgrade()。

SQLiteOpenHelper中还有两个非常重要的实例方法,getReadableDatabase()和getWritableDatabase()。

在文章最后仍然会附上DEMO.

先附上一张图

SQLite的使用------图片存储_第1张图片

一、此例与文本例互相独立,继承 SQLiteOpenHelper 类实现的 dbHelper 类。

 

 

public class BitmpUtil extends SQLiteOpenHelper {

	public BitmpUtil(Context context) {
		super(context, "djf", null, 2);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		db.execSQL("Create table img ( id INTEGER PRIMARY KEY AUTOINCREMENT,uname TEXT,uimgae BLOB );");  
	}

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

二、主界面中选择相册中图片,存储,预览

public class MainActivity extends Activity {

    private Button btnSave, btnQuery;

    private ImageView selectIcon;
    private ImageView showIcon;

    /*数据库操作*/
    private BitmpUtil bu;

    private Bitmap bit;

	
	/*数据库操作*/


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bu = new BitmpUtil(MainActivity.this);

        btnSave = (Button) findViewById(R.id.save);

        btnQuery = (Button) findViewById(R.id.query);

        selectIcon = (ImageView) findViewById(R.id.iv_icon);
        showIcon = (ImageView) findViewById(R.id.iv_showIcon);


        /**
         * 选择图片
         */
        selectIcon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "image/*");
                startActivityForResult(intent, 0x1);
            }
        });

        /**
         * 增加
         */
        btnSave.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SQLiteDatabase db = bu.getWritableDatabase();

                ContentValues values = new ContentValues();
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                bit.compress(Bitmap.CompressFormat.PNG, 100, os);
                values.put("uimgae", os.toByteArray());
                values.put("uname", "zhangsan");
                db.insert("img", null, values);
                db.close();
                Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_SHORT).show();
            }
        });


        /**
         * 显示图片
         */
        btnQuery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                SQLiteDatabase db = null;
                db = bu.getReadableDatabase();
                Cursor c = db.query("img", new String[]{"id", "uname", "uimgae"}, "uname=?", new String[]{"zhangsan"}, null, null, null);
                while (c.moveToNext()) {
                    String id = c.getString(c.getColumnIndex("id"));
                    String name = c.getString(c.getColumnIndex("uname"));
                    byte[] in = c.getBlob(c.getColumnIndex("uimgae"));
                    Bitmap bit = BitmapFactory.decodeByteArray(in, 0, in.length);
                    showIcon.setImageBitmap(bit);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 0x1 && resultCode == RESULT_OK) {
            Uri u = data.getData();
            ContentResolver cr = MainActivity.this.getContentResolver();
            try {
                bit = BitmapFactory.decodeStream(cr.openInputStream(u));
                selectIcon.setImageBitmap(bit);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

三、存储核心代码

 SQLiteDatabase db = bu.getWritableDatabase();
                ContentValues values = new ContentValues();
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                bit.compress(Bitmap.CompressFormat.PNG, 100, os);
                values.put("uimgae", os.toByteArray());
                values.put("uname", "zhangsan");
                db.insert("img", null, values);
                db.close();
SQLiteDatabase db = bu.getWritableDatabase();获取数据库,使用ContentValues的对象进行数据插入,使用流写入,最后关闭数据库。

四、读取存储中图片预览

 SQLiteDatabase db = null;
                db = bu.getReadableDatabase();
                Cursor c = db.query("img", new String[]{"id", "uname", "uimgae"}, "uname=?", new String[]{"zhangsan"}, null, null, null);
                while (c.moveToNext()) {
                    String id = c.getString(c.getColumnIndex("id"));
                    String name = c.getString(c.getColumnIndex("uname"));
                    byte[] in = c.getBlob(c.getColumnIndex("uimgae"));
                    Bitmap bit = BitmapFactory.decodeByteArray(in, 0, in.length);
                    showIcon.setImageBitmap(bit);
                }
            }
        });

主要代码已贴

百度网盘:  下载         提取码:aupa

CSDN:下载

下一篇:SQLite多张图片存储

你可能感兴趣的:(SqlLite的使用)