废话不多说,直接粘贴代码上来首先建一个数据库类:
import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteDatabase.CursorFactory;import androID.database.sqlite.sqliteOpenHelper;public class Helpersave extends sqliteOpenHelper{ public Helpersave(Context context,String name,CursorFactory factory,int version) { super(context,name,factory,version); // Todo auto-generated constructor stub } public Helpersave(Context context) { // Todo auto-generated constructor stub super(context,"SaveImageDemo",null,1);//这个构造器必须有,activity里会调用; } @OverrIDe public voID onCreate(sqliteDatabase jb) { // Todo auto-generated method stub jb.execsql("create table image(image_ID integer primary key,bit_image BLOB)"); } @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // Todo auto-generated method stub } }</span>activity:
<span >package com.example.sampleadapterdemo;import java.io.ByteArrayOutputStream;import java.io.inputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import androID.app.Activity;import androID.content.ContentValues;import androID.content.Intent;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.net.Uri;import androID.nfc.Tag;import androID.os.Bundle;import androID.provIDer.MediaStore;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.ImageVIEw;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;public class MainActivity extends Activity { private static int RESulT_LOAD_IMAGE; private static final String TAG = "Result"; MyHelper helper; sqliteDatabase db; Cursor cursor; private ListVIEw ListVIEw =null; private SimpleAdapter adapter ; private button but1; private button but2; private ImageVIEw Image; private List<Map<String,Object>> List = new ArrayList<Map<String,Object>>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Image = (ImageVIEw) findVIEwByID(R.ID.Image1); but1 = (button) super.findVIEwByID(R.ID.but1); but2 = (button) super.findVIEwByID(R.ID.but2); but2.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub db = helper.getWritableDatabase(); cursor = db.query("image",null); if (cursor.movetoFirst()) { // byte[] —> Bitmap byte[] bytes = cursor.getBlob(cursor.getColumnIndex("bit_image")); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,bytes.length,null); Image.setimageBitmap(bitmap); cursor.close(); db.close(); helper.close(); } } }); but1.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub //一个重定向打开系统图库 Intent i = new Intent( Intent.ACTION_PICK,androID.provIDer.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i,RESulT_LOAD_IMAGE); } }); ListVIEw=(ListVIEw) super.findVIEwByID(R.ID.ListvIEw1); String str1="001"; Object image1 =R.drawable.food_image; Map<String,Object> map = new HashMap<String,Object>(); map.put(str1,image1); List.add(map); adapter = new SimpleAdapter(getApplicationContext(),List,R.layout.List,new String[]{str1},new int[]{R.ID.image}); ListVIEw.setAdapter(adapter); } @OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent data) { // Todo auto-generated method stub super.onActivityResult(requestCode,resultCode,data); if (requestCode == RESulT_LOAD_IMAGE && resultCode == RESulT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; cursor = getContentResolver().query(selectedImage,filePathColumn,null); cursor.movetoFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); Log.d(TAG,"已经得到bitmap"); //Image.setimageBitmap(BitmapFactory.decodefile(picturePath)); //从手机选择图片插入数据库 Bitmap bitmap = BitmapFactory.decodefile(picturePath); byte[] by = bitmapToBytes(bitmap); //cursor = db.query("image",null); ContentValues values = new ContentValues(); values.put("image_ID",17); values.put("bit_image",by); db = helper.getWritableDatabase(); db.insert("image",values);//插入数据 db.close(); helper.close(); } } public static byte[] bitmapToBytes(Bitmap bitmap){ if (bitmap == null) { return null; } final ByteArrayOutputStream os = new ByteArrayOutputStream(); // 将Bitmap压缩成PNG编码,质量为100%存储 bitmap.compress(Bitmap.CompressFormat.JPEG,100,os);//除了PNG还有很多常见格式,如jpeg等。 return os.toByteArray(); } }</span>
最后布局文件:
<span ><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/hello_world" androID:visibility="gone"/> <button androID:ID="@+ID/butopen" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"/> <button androID:ID="@+ID/butsave" androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" /> <button androID:ID="@+ID/butshow" androID:layout_height="wrap_content" androID:layout_wIDth="match_parent"/> <ImageVIEw androID:ID="@+ID/Image" androID:layout_height="wrap_content" androID:layout_wIDth="wrap_content" /> <ImageVIEw androID:ID="@+ID/imageshow" androID:layout_height="wrap_content" androID:layout_wIDth="wrap_content"/></linearLayout></span>总结
以上是内存溢出为你收集整理的用sqlite存储Android手机图片,再从数据库读出图片显示。全部内容,希望文章能够帮你解决用sqlite存储Android手机图片,再从数据库读出图片显示。所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)