5

I want to save image in sqlite. In this method I have got image path into string. My Question is How to convert it into byte[] format.

if (resultCode == RESULT_OK) {
// we need to update the gallery by starting MediaSanner service.
mScanner = new MediaScannerConnection(ImagePreviewActivity.this,new MediaScannerConnection.MediaScannerConnectionClient() {

                        public void onMediaScannerConnected() {
                                mScanner.scanFile(imageUri.getPath(), null );
                        }           

                        public void onScanCompleted(String path, Uri uri) {
                            mScanner.disconnect();
                            if (path.equals(imageUri.getPath())) {
                                 photoPath=imageUri.getPath();
                                        ImagePreviewActivity.this.runOnUiThread(new Runnable() {
                                        public void run() {
                                            capture_photo = (ImageView) findViewById(R.id.caturePhoto);
                                            Bitmap myBitmap = BitmapFactory.decodeFile(photoPath);
                                            capture_photo.setImageBitmap(myBitmap);

                                    }
                                });
                                mScanner.disconnect();
                            }
                        }
                    });
                    mScanner.connect();
Zied R.
  • 4,964
  • 2
  • 36
  • 67
Ash
  • 682
  • 2
  • 10
  • 20
  • this can hep you : http://stackoverflow.com/questions/10831151/how-to-store-and-retrieve-images-in-android-sqlite-database-and-display-them-on – Zied R. Mar 01 '14 at 16:01
  • possible duplicate of [Convert image to byte array and viceversa](http://stackoverflow.com/questions/15805146/convert-image-to-byte-array-and-viceversa) – Devrim Mar 01 '14 at 16:18
  • I would just store the compressed image data directly from the file http://stackoverflow.com/questions/858980/file-to-byte-in-java - not the decoded pixel data since that a) saves space b) is simpler to load as `Bitmap` (via `BitmapFactory`), i.e. dump file you get via `photoPath` into the database without decoding it. – zapl Mar 01 '14 at 16:20

3 Answers3

9

First convert ImagePath to Bitmap

       Bitmap src=BitmapFactory.decodeFile(imagePath);

Convert bitmap into byte array using this code:

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        src.compress(Bitmap.CompressFormat.PNG, 100, baos);
        data = baos.toByteArray();

After your question in comment

To show byte array into imageview use these lines of code:

   Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);

    imageView.setImageBitmap(bitmap);

To show imagePath into imageView you can use

                 Bitmap src=BitmapFactory.decodeFile(imagePath);
                 imageView.setImageBitmap(bitmap);
Abhishek Agarwal
  • 1,907
  • 12
  • 21
  • Thanks, Can you tell me how Convert byte array into bitmap and then convert Bitmap to Image – Ash Mar 01 '14 at 16:22
  • you can directly save byte array into image – Abhishek Agarwal Mar 01 '14 at 16:30
  • Actually I want to fetch Image from Sqlite and show it in imageview. So, In what method we have to use for it – Ash Mar 01 '14 at 16:32
  • thanks again, but my main problem is I want to read from sqlite"while(cursor.moveToNext()){ cimages.add(new CImage((cursor.getBlob(cursor.getColumnIndex(CAMERA_PHOTO))))); } how can use method to read it from sqlite ? – Ash Mar 01 '14 at 18:04
2

Like this:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
0

this function convert an image into array byte[]:

 public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}
Zied R.
  • 4,964
  • 2
  • 36
  • 67