-2

I want to create a form where the person can save his data with upload image option. And save all data in SQLiteDatabase.

Thanks in Advance

Deepshikha Puri
  • 2,104
  • 22
  • 23

2 Answers2

0

Check this answer storing image into SQLite in android but actually storing images in database not a good practice. Better save image on SD Card and insert into DB only path to this image.

Official tutorial how to save data in SQLite http://developer.android.com/training/basics/data-storage/databases.html

Community
  • 1
  • 1
Yurets
  • 3,999
  • 17
  • 54
  • 74
  • When I saved the image using uri path its show me this error after click on uri path: – Deepshikha Puri Jan 17 '15 at 04:54
  • 01-17 10:23:36.312: E/AndroidRuntime(22875): FATAL EXCEPTION: main 01-17 10:23:36.312: E/AndroidRuntime(22875): java.lang.NullPointerException 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.content.ContentResolver.openInputStream(ContentResolver.java:436) 01-17 10:23:36.312: E/AndroidRuntime(22875): at com.example.uripath.MainActivity$4.onClick(MainActivity.java:98) 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.view.View.performClick(View.java:4452) – Deepshikha Puri Jan 17 '15 at 04:55
  • 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.view.View$PerformClick.run(View.java:18428) 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.os.Handler.handleCallback(Handler.java:725) 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.os.Handler.dispatchMessage(Handler.java:92) 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.os.Looper.loop(Looper.java:176) 01-17 10:23:36.312: E/AndroidRuntime(22875): at android.app.ActivityThread.main(ActivityThread.java:5365) – Deepshikha Puri Jan 17 '15 at 04:56
  • @user3881947 , post please your code as edit to your main question – Yurets Jan 17 '15 at 04:56
  • 01-17 10:23:36.312: E/AndroidRuntime(22875): at java.lang.reflect.Method.invokeNative(Native Method) 01-17 10:23:36.312: E/AndroidRuntime(22875): at java.lang.reflect.Method.invoke(Method.java:511) 01-17 10:23:36.312: E/AndroidRuntime(22875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) – Deepshikha Puri Jan 17 '15 at 04:57
  • 01-17 10:23:36.312: E/AndroidRuntime(22875): at java.lang.reflect.Method.invokeNative(Native Method) 01-17 10:23:36.312: E/AndroidRuntime(22875): at java.lang.reflect.Method.invoke(Method.java:511) 01-17 10:23:36.312: E/AndroidRuntime(22875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) – Deepshikha Puri Jan 17 '15 at 04:57
  • 01-17 10:23:36.312: E/AndroidRuntime(22875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 01-17 10:23:36.312: E/AndroidRuntime(22875): at dalvik.system.NativeStart.main(Native Method) – Deepshikha Puri Jan 17 '15 at 04:58
  • @user3881947, I'm sorry, how do you want me to read it? Edit your question and post everything there. All these comments remove please, nobody does like this. – Yurets Jan 17 '15 at 04:58
0

You can insert image in byte array format in sqlite. To store byte array you need to define a column with blob type format.

and then you can simply do like that :-

public void storeImage(Bitmap img){
    byte[] data = getBitmapAsByteArray(img);     

     SQLiteDatabase db=database.getWritableDatabase();
     db.execSQL("INSERT INTO IMAGETABLE VALUES(data);");
}

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42