title: How to insert image into SQLite Database
body: <p>I am trying to insert image into <code>SQLite</code> data base but i am getting <code>null</code> point exception, But checked there is no any <code>null object</code>. I tried my level best but still i cannot find the  problem help me. My tried code is shown below. Thank you stack overflow and help me please,</p>
  
  <h1>MainActivity.class</h1>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  ImageView imageView;
  Button button;
  Bitmap bitmap;
  Context context;
  
  LocalDatabase localDatabase;
  public static final int PICK_IMAGE = 1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      imageView=(ImageView)findViewById(R.id.imageid);
      localDatabase=new LocalDatabase(this);
      button=(Button)findViewById(R.id.btnid);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
          }
      });
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
      if (requestCode == PICK_IMAGE) {
          Uri uri=data.getData();
          imageView.setImageURI(uri);
          InputStream iStream = null;
          try {
              iStream = getContentResolver().openInputStream(uri);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          try {
              byte[] inputData = getBytes(iStream);
              saveimage(inputData);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }
  }
  public byte[] getBytes(InputStream inputStream) throws IOException {
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
      int bufferSize = 1024;
      byte[] buffer = new byte[bufferSize];
  
      int len = 0;
      while ((len = inputStream.read(buffer)) != -1) {
          byteBuffer.write(buffer, 0, len);
      }
      return byteBuffer.toByteArray();
  }
  boolean saveimage(byte[] bytes)
  {
      localDatabase.insertImage(bytes);
      return true;
  }
  }
  </code></pre>
  
  <h1>LocalDataBase.class</h1>
  
  <pre><code>public class LocalDatabase extends SQLiteOpenHelper {
  public static final String IMAGE_ID = "id";
  public static final String IMAGE = "image";
  LocalDatabase localDatabase;
  
  
  private SQLiteDatabase mDb;
  
  private static final String DATABASE_NAME = "Images.db";
  private static final int DATABASE_VERSION = 1;
  
  private static final String IMAGES_TABLE = "ImagesTable";
  
  private static final String CREATE_IMAGES_TABLE =
          "CREATE TABLE " + IMAGES_TABLE + " (" +
                  IMAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                  + IMAGE + " BLOB NOT NULL );";
  
  public LocalDatabase(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  
  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {
      mDb.execSQL(CREATE_IMAGES_TABLE);
  
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
      sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + CREATE_IMAGES_TABLE);
      onCreate(sqLiteDatabase);
  }
  
  // Insert the image to the Sqlite DB
  public void insertImage(byte[] imageBytes) {
       mDb=localDatabase.getWritableDatabase();
      ContentValues cv = new ContentValues();
      cv.put(IMAGE, imageBytes);
      mDb.insert(IMAGES_TABLE, null, cv);
      localDatabase.close();
  }
  
  // Get the image from SQLite DB
  // We will just get the last image we just saved for convenience...
  public byte[] retreiveImageFromDB() {
      Cursor cur = mDb.query(true, IMAGES_TABLE, new String[]{IMAGE,},
              null, null, null, null,
              IMAGE_ID + " DESC", "1");
      if (cur.moveToFirst()) {
          byte[] blob = cur.getBlob(cur.getColumnIndex(IMAGE));
          cur.close();
          return blob;
      }
      cur.close();
      return null;
  }
  }
  </code></pre>