Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

android - How to store images in sqlite database on click event of button?

How to store image in sqlite database on click event of button?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
package com.examples.aMySQL2;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


//Reading lines from a file
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;


public class aMySQL2Activity extends Activity implements OnClickListener
{
    protected static TextView textView;
    protected static ImageView bmImage;
    protected Button start;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bmImage = (ImageView)findViewById(R.id.imageView1);
        textView = (TextView) findViewById(R.id.textView1);

        start = (Button) findViewById(R.id.button1);
        start.setOnClickListener(this); 

    DownloadFile();


    }
    // /data/data/<package_name>/databases  -путь к базе данных по умолчанию
    // "/data/data/com.examples.aMySQL2/databases/MyDB.db" //-путь к базе данных по умолчанию
    //  "/sdcard/Nick/MyDB.db"
    //  "/mnt/sdcard/Nick/MyDB.db"
    /// Android/data/<package_name>/files/
    public void onClick(View v) 
    {
     SQLiteDatabase myDb;       
     String MySQL;
     int icount;
     byte[] byteImage1 = null;
     byte[] byteImage2 = null;
       MySQL="create table emp1(_id INTEGER primary key autoincrement, fio TEXT not null, picture BLOB);";
       myDb = openOrCreateDatabase("/sdcard/Nick/MyWeatherDB.db", Context.MODE_PRIVATE, null);
  //     myDb.execSQL(MySQL);
  String s=myDb.getPath();
  textView.append("
" + s+"
");       
       myDb.execSQL("delete from emp1");
       ContentValues newValues = new ContentValues();
    newValues.put("fio", "Иванов Петр Сергеевич");

 /////////// insert picture to blob field ///////////////////// 
    try
    {
    FileInputStream instream = new FileInputStream("/sdcard/Nick/weather.png"); 
    BufferedInputStream bif = new BufferedInputStream(instream); 
    byteImage1 = new byte[bif.available()]; 
    bif.read(byteImage1); 
textView.append("
" + byteImage1.length+"
"); 
    newValues.put("picture", byteImage1); 

    long ret = myDb.insert("emp1", null, newValues); 
    if(ret<0) textView.append("
!!! Error add blob filed!!!
");
    } catch (IOException e) 
    {
        textView.append("
!!! Error: " + e+"!!!
");   
    }

////////////Read data ////////////////////////////  
Cursor cur = myDb.query("emp1",null, null, null, null, null, null);
    cur.moveToFirst();
    while (cur.isAfterLast() == false)
    {
        textView.append("
" + cur.getString(1)+"
");
        cur.moveToNext();
    }
///////Read data from blob field////////////////////
    cur.moveToFirst();
    byteImage2=cur.getBlob(cur.getColumnIndex("picture")); // ИМЕННО ТАК!!!
bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));
    textView.append("
" + byteImage2.length+"
"); 
//////////////////////////    
    cur.close();
    myDb.close();
  }

    public void DownloadFile() 
    {   
      Bitmap bitmap1 = null;                    
      bitmap1 = BitmapFactory.decodeFile("/sdcard/Nick/saranka.jpg"); //weather.png");
      bmImage.setImageBitmap(bitmap1);
    }


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...