日期:2014-05-16  浏览次数:20419 次

Android中SQlite数据库存取Bitmap(一)

mainActivity如下:

package cn.com;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import cn.db.DBservice;
import cn.db.Photo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private Button mButton;
    private Bitmap mBitmap;
    private ImageView mImageView;
    private boolean isDownload=true;
    private boolean isSave=false;
    private boolean isLoadFromDB=false;
    private static final int DOWNLOAD_FINISH=88;
    private byte[] photoByte;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton=(Button) findViewById(R.id.button);
        mButton.setOnClickListener(new ButtonOnClickListener());
        mImageView=(ImageView) findViewById(R.id.imageView);
    }
   
   private class ButtonOnClickListener implements View.OnClickListener{
	public void onClick(View v) {
		if (isDownload) {
			new Thread(){
				public void run() {
					mBitmap=getBitmap("http://s12.sinaimg.cn/middle/4b181bd0gbf6cb07c2a1b&690");
					handler.sendEmptyMessage(DOWNLOAD_FINISH);
				}
			}.start();
		}
		if (isSave) {
			DBservice service=new DBservice(MainActivity.this);
			Photo photo=new Photo(1,mBitmap);
			service.save(photo,photoByte);
			isLoadFromDB=true;
			isSave=false;
			mButton.setText("从数据库中加载bitmap");
			mImageView.setImageBitmap(null);
			return;
		}
		if (isLoadFromDB) {
			DBservice service=new DBservice(MainActivity.this);
			Photo photo=service.find(1);
			mImageView.setImageBitmap(photo.getPhoto());
		}
	 }
   }
   
   Handler handler=new Handler(){
   	public void handleMessage(Message msg) {
   		switch (msg.what) {
			case DOWNLOAD_FINISH:
				mImageView.setImageBitmap(mBitmap);
				mButton.setText("保存图片至SQLite");
				isDownload=false;
				isSave=true;
				break;
			default:
				break;
			}
   	};
   };
   
	public Bitmap getBitmap(String imagePath) {
		try {
			URL imageUrl = new URL(imagePath);
			HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
			connection.setConnectTimeout(5000);
			connection.setRequestMethod("POST");
			if (connection.getResponseCode() == 200) {
				InputStream inputStream = connection.getInputStream();
				photoByte= readResource(inputStream);
				Bitmap bitmap=BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
				return bitmap;
			}
		} catch (Exception e) {
		}
		
		return null;
	};
   public byte[] readResource(InputStream inputStream){
	   try {
		   ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
	    	byte [] buffer=new byte[1024];
	    	int len=0;
	    	while( (len=inputStream.read(buffer))!=-1){
	    		   outputStream.write(buffer,0,len);
	    	}
	    	inputStream.close();
	    	outputStream.close();   	
	    	return outputStream.toByteArray();
	} catch (Exception e) {
	}
    return null;
   }
}

DataBaseOpenHelper.java如下:

package cn.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper extends SQLiteOpenHelper {


	public DataBaseOpenHelper(Context context) {
		super(context, "testbold.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table test(photoid integer primary key autoincrement,id ingeter(10),photo Blob(20))");
	}

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

}


 


DBservice.