How to store an image file to SQliteDB in android
2010-06-17 16:01
531 查看
根据上一篇转载内容我自己尝试着把一个image文件存入到sqlite中,并且把它读出来。
从数据库中读出的文件放在以下路径:/data/data/packagename/files。
完整代码如下:
从数据库中读出的文件放在以下路径:/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."); } // save the image to database 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(); } } } // get the image file from database 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; } } }
相关文章推荐
- How to store an image file to SQliteDB in android
- How to load an ImageView by URL in Android
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- How to install an APK file in the Android emulator?
- git tip: How to undo an uncommitted change/deletion for a specific file in your working directory
- How to scroll an image, if it does not fit in the display
- Android Tricks: How to Decompile an Apk file
- How to tile a background image in Android
- How to create an OCM Response file to apply patch in silent mode - opatch silent (Doc ID 966023.1)
- How to make a USB stick use ISO image file in debian
- How to Use FontAwesome in an Android App(把 icons 变得和 text 一样)
- how to make a authorware file in an environment without authorware
- How to Render Image Buffer in Android NDK Native Code
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- 如何挂载一个镜像文件(how to mount an image file)
- How to Pop Open an image window that resizes to fit the image in ASP.net 1.x and 2.0 Beta 1
- How to insert a SWF file in an ASDoc API documentation ?
- How To Open An URL In Android’s Web Browser
- How to Get an Android Device Image?
- How to build an OLE DB application for SQL Server CE in Visual Studio 2005