Thursday, 8 August 2013

Android storing picture in mySql database and downloading BLOB as JPEG from phpMyAdmin

Android storing picture in mySql database and downloading BLOB as JPEG
from phpMyAdmin

I am currently trying to upload a picture taken from the camera of a
physical Android device to a MySql database.
I first fetch the BitMap of the picture taken, then convert it to a byte
array, then base64 encode it so that I can pass it as a string in a
NameValuePair to my database. The string is stored as a MEDIUMBLOB in the
database. This is the code that does this.
Start the camera app
Intent cameraIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
onActivityResult
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
imgString = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
The imgString is then added to an ArrayList as a NameValuePair and sent to
the database using makeHttpRequest.
The code above works fine and the data is added correctly.
What I am trying to do though is allow the user to download the photo as a
JPEG inside phpMyAdmin when the [BLOB - x KiB] for a certain row is
clicked. What I am currently getting is a .bin file which when opened
gives a .cpgz which in return gives a .bin and so on...
I am not entirely sure if I am missing a step or doing something wrong. I
did read that it may be better to store a file path to the picture instead
of the actual picture, but I'd rather
Thanks in advance for any help provided.

No comments:

Post a Comment