How to store an image file to SQliteDB in android
根据上一篇转载内容我自己尝试着把一个image文件存入到sqlite中,并且把它读出来。
从数据库中读出的文件放在以下路径:/data/data/packagename/files。
完整代码如下:
package com.test.image;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class ImageStoreActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String DB_NAME = "mp3Song.db";
private static final int DB_VERSION = 2;
private Button btn, btn2, btn3;
private Cursor cur;
private MediaPlayer mPlayer;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this);
btn.setId(101);
btn.setText("show");
// btn.setBackgroundResource(resid);
btn.setOnClickListener(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(80, 50);
param.topMargin = 10;
layout.addView(btn, param);
setContentView(layout);
btn2 = new Button(this);
btn2.setId(102);
btn2.setText("retrieve");
btn2.setOnClickListener(this);
layout.addView(btn2, param);
// init();
setTitle("Saving into SQliteDB.");
}
private DatabaseHelper mOpenHelper;
public void init() {
mOpenHelper = new DatabaseHelper(this);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql_drop = "drop table IF EXISTS imagetable";
db.execSQL(sql_drop);
String sql_create = "create table imagetable("
+ "pic_id text not null, " + "pic_detail blob);";
db.execSQL(sql_create);
SaveOneSong(db, "s01", R.raw.beauty);
db.close();
setTitle("saved in SQLiteDB.");
}
public void SaveOneSong(SQLiteDatabase db, String key, int rid) {
ContentValues cv = new ContentValues();
cv.put("pic_id", key);
InputStream ins = getResources().openRawResource(rid);
byte[] buffer = new byte[63 * 1024];
try {
int size = ins.read(buffer);
while (size > 0) {
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
out.write(buffer, 0, size);
out.flush();
out.close();
cv.put("pic_detail", out.toByteArray());
db.insert("imagetable", null, cv);
size = ins.read(buffer);
}
} catch (Exception e) {
} finally {
try {
ins.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void retrieve(String cond) {
FileOutputStream os = null;
try {
os = openFileOutput("xixi.jpg", MODE_WORLD_READABLE);
} catch (Exception e) {
}
byte red_buf[];
mOpenHelper = new DatabaseHelper(this);
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = {"pic_id", "pic_detail"};
Cursor c = db.query("imagetable", col, cond, null, null, null, null);
int k = 0;
c.moveToFirst();
try {
while (!c.isAfterLast()) {
red_buf = c.getBlob(1);
os.write(red_buf);
k++;
c.moveToNext();
}
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case 101:
init();
break;
case 102:
retrieve("pic_id='s01'");
break;
}
}
}
原文:http://xiaoxixi615.blog.sohu.com/142058403.html
更多相关文章
- 答复: Android中使用硬盘模拟SD卡
- AndroidStudio插件:布局文件转化Databinding
- 【转】【Android】获取手机中已安装apk文件信息(PackageInfo、Re
- android4.0编译错误集(一)
- Android(安卓)无cp命令 mv引起cross-device link
- Android(安卓)Gradle 指定 Module 打包
- Android(安卓)3D引擎之min3D--支持材质和UV贴图的objloader
- Android(安卓)源代码编后的目录分析
- Android(安卓)ndk r4b开发环境搭建