Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 1.88 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. picture isn't saved properly into the database (as BLOB)
  2. public boolean SQL_INSERT_PICTURE(PictureDatatype type) {
  3.     try{
  4.         this.db = openHelper.getWritableDatabase();
  5.         Log.d(TAG, "Insert picture");
  6.         System.out.println("insert picture");
  7.         db.execSQL("DELETE FROM picture " +
  8.                 "where " + LinkPic + " = '" + type.getLink() + "'");
  9.         db.execSQL("INSERT OR REPLACE INTO picture " +
  10.                 "( " + NamePic + ", " + LinkPic + ", " + PicturePic + ") " +
  11.                 "VALUES ('" + type.getName() + "', '" + type.getLink() +"', '" +
  12.                 type.getPicture() +"')");
  13.  
  14.         System.out.println("size of the picture (bytearray): " + type.getPicture().length);
  15.         System.out.println("the picture: " + type.getPicture());
  16.         System.out.println("insert complete");
  17.         db.close();
  18.         return true;
  19.     }
  20.         catch(SQLException s){
  21.             System.out.println("Insert failed");
  22.             db.close();
  23.             return false;
  24.         }
  25. }
  26.        
  27. private byte[] getBitmapAsByteArray(Bitmap bitmap) {
  28.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  29.         // Middle value is quality, but PNG is lossless, so it's ignored.
  30.         bitmap.compress(CompressFormat.PNG, 0, outputStream);
  31.         return outputStream.toByteArray();
  32.     }
  33.        
  34. private byte[] getBitmapAsByteArray(Bitmap bitmap) {
  35.         final int width = bitmap.getWidth();
  36.         final int height = bitmap.getHeight();
  37.         ByteBuffer byteBuffer = ByteBuffer.allocate(width * height * 4);
  38.  
  39.         bitmap.copyPixelsToBuffer(byteBuffer);
  40.         return byteBuffer.array();
  41.     }
  42.        
  43. public Bitmap loadIcon(long iconId) {
  44.        // Prepare the cursor to read from database....
  45.        byte[] bitmapData = cursor.getBlob(cursor.getColumnIndex(Columns.Icons.DATA));
  46.  
  47.        return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
  48.    }