Insertion byte array Image Sqlite
Bonjour j'essaye dinserer une byte array dans mon bdd cependant cela ne marche pas ni avec une methode d'insertion classique ni avec un blob
voici mon code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
//Methode 1
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myicontest); //jute un exemple
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();
myDb = openOrCreateDatabase("mybd", MODE_PRIVATE, null);
myDb.execSQL("INSERT INTO Picture(JobId, Picture, date, user ) VALUES (" +jobId + ", '@" +
bitmapdata + "', '" + utils.GetDate() +"', " + userId +")" );
//Methode 2
SQLiteStatement insertStmt = myDb.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1,jobId);
insertStmt.bindBlob(2,bitmapdata );
insertStmt.bindString(3,utils.GetDate());
insertStmt.bindString(4,userId);
insertStmt.executeInsert();
//Exception
02-27 14:41:56.376: D/AndroidRuntime(13879): Shutting down VM
02-27 14:41:56.376: W/dalvikvm(13879): threadid=1: thread exiting with uncaught exception (group=0x4125c2a0)
02-27 14:41:56.376: E/AndroidRuntime(13879): FATAL EXCEPTION: main
02-27 14:41:56.376: E/AndroidRuntime(13879): java.lang.NullPointerException
02-27 14:41:56.376: E/AndroidRuntime(13879): at com.example.myappwng.SignAndPic$3.onClick(SignAndPic.java:178)
02-27 14:41:56.376: E/AndroidRuntime(13879): at android.view.View.performClick(View.java:4222)
02-27 14:41:56.376: E/AndroidRuntime(13879): at android.view.View$PerformClick.run(View.java:17273)
02-27 14:41:56.376: E/AndroidRuntime(13879): at android.os.Handler.handleCallback(Handler.java:615)
02-27 14:41:56.376: E/AndroidRuntime(13879): at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 14:41:56.376: E/AndroidRuntime(13879): at android.os.Looper.loop(Looper.java:137)
02-27 14:41:56.376: E/AndroidRuntime(13879): at android.app.ActivityThread.main(ActivityThread.java:4895)
02-27 14:41:56.376: E/AndroidRuntime(13879): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 14:41:56.376: E/AndroidRuntime(13879): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 14:41:56.376: E/AndroidRuntime(13879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
02-27 14:41:56.376: E/AndroidRuntime(13879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
02-27 14:41:56.376: E/AndroidRuntime(13879): at dalvik.system.NativeStart.main(Native Method)
02-27 14:42:09.509: I/Process(13879): Sending signal. PID: 13879 SIG: 9 |
//Limage recuperer nest pas null.
avez vous des idees pour linsertion des images ou des pistes de recherches pour resoudre ce probleme? ai-je omis une manipulation supplementaires ?
Je vous remercie par avance
un exemple image to sqlite
main_activity.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="64dp"
android:layout_toRightOf="@+id/textView1" />
<Button
android:id="@+id/button_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView_image"
android:layout_marginTop="45dp"
android:layout_toRightOf="@+id/textView1"
android:text="Insert In DB" />
<Button
android:id="@+id/button_retrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button_insert"
android:layout_below="@+id/button_insert"
android:layout_marginTop="56dp"
android:text="Retrieve from DB" />
</RelativeLayout> |
MainActivity.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package com.sunil.insertimageindb;
import java.io.ByteArrayOutputStream;
import com.sunil.insertimageindb.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private ImageView imageview=null;
private Button btninsert=null;
private Button btnretrive=null;
private MyDataBase mdb=null;
private SQLiteDatabase db=null;
private Cursor c=null;
private byte[] img=null;
private static final String DATABASE_NAME = "ImageDb.db";
public static final int DATABASE_VERSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btninsert=(Button)findViewById(R.id.button_insert);
btnretrive= (Button)findViewById(R.id.button_retrieve);
imageview= (ImageView)findViewById(R.id.imageView_image);
imageview.setImageResource(0);
btninsert.setOnClickListener(this);
btnretrive.setOnClickListener(this);
mdb=new MyDataBase(getApplicationContext(), DATABASE_NAME,null, DATABASE_VERSION);
Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
img=bos.toByteArray();
db=mdb.getWritableDatabase();
}
@Override
public void onClick(View arg0) {
if(btninsert==arg0)
{
ContentValues cv=new ContentValues();
cv.put("image", img);
db.insert("tableimage", null, cv);
Toast.makeText(this, "inserted successfully", Toast.LENGTH_SHORT).show();
}
else if(btnretrive==arg0)
{
String[] col={"image"};
c=db.query("tableimage", col, null, null, null, null, null);
if(c!=null){
c.moveToFirst();
do{
img=c.getBlob(c.getColumnIndex("image"));
}while(c.moveToNext());
}
Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length);
imageview.setImageBitmap(b1);
Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();
}
}
} |
MyDataBase.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
package com.sunil.insertimageindb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class MyDataBase extends SQLiteOpenHelper{
public MyDataBase(Context context, String dbname, CursorFactory factory, int dbversion) {
super(context, dbname, factory, dbversion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tableimage(image blob);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
} |