How to store an image file to SQliteDB in android
来源:互联网 发布:缠论123战法源码 编辑:程序博客网 时间:2024/05/17 23:14
根据上一篇转载内容我自己尝试着把一个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
- How to store an image file to SQliteDB in android
- How to store an image file to SQliteDB in android
- How to mount an image file.
- How to install an APK file in the Android emulator?
- How to Get an Android Device Image?
- How to center an image?
- How to transfer image file to server from android
- How to use an external dll file in VisualStudio project
- Android Tricks: How to Decompile an Apk file
- How to load an url image
- How To Upload An Image into BSP
- How to store an Object into an integer?
- How to tile a background image in Android
- How to Render Image Buffer in Android NDK Native Code
- How to Render Image Buffer in Android NDK Native Code
- How to get file path in onActivityResult in Android 4.4
- How to store node content in Drupal
- How to store CGRect values in NSMutableArray?
- 关于VO、PO的理解-java的(PO,VO,TO,BO,DAO,POJO)解释(转)
- 百度员工披露"有啊"创始人李明远离职内幕
- 如何往数据库中存储mp3,image等文件
- Embedded System
- 可以选择时分秒的JS日历控件
- How to store an image file to SQliteDB in android
- Sql Server 获得各种形式的日期
- BLOB转换为CLOB的函数(oracle中执行)
- Linux 内核修炼之道, 现在还来不及看. 之后看看是怎么回事.
- SvnAnt authentication cancelled 的解决
- Eclipse Dynamic Web Project、Static Web Project、Web Fragment Project的区别
- 最受老板欢迎的十大技能
- c#各种排序算法
- 如何在MySQL&Oracle下创建自动递增字段