1

I am working on a project where I need to upload images to the database and retrieve the same from database.

I need the image to be uploaded from an android device using java. Here is what i have implemented

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte[] byte_arr = stream.toByteArray();
            image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

I am inserting the byte array to the database. And here is my php code to retrieve the same :

 /**
*Get profile_pic*/
public function callmethod($userId){
    $stmt = $this->conn->prepare("SELECT profile_pic FROM users WHERE unique_id=?");
    $stmt->bind_param('s',$userId); 
    //$result = mysql_query($query) or die(mysql_error()); 
    //$photo = mysql_fetch_array($result); 
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($profile_pic);
    $stmt->fetch();
     //$obj->picture = base64_encode($profile_pic);
    //echo $obj;

    header("Content-Type: image/jpeg");
echo '<img src="data:image/jpeg;base64,<?php echo base64_encode($profile_pic); ?>" />';
}

The problem that I am facing here is that the files are getting uploaded to the database but when I am retrieving the image from the database the variable $profile_pic is empty, hence the image is not being displayed.

I need it to be able to retrieve the image using java in android. can I do that by just encoding the value retrieve with json format?

Pleas let me know what I am doing wrong. TIA

2
  • The answer is you are making your life needlessly complicated by storing files in a database. It's simpler and more efficient (both from coding and storage/retrieval perspective) to store the files on the file system. Commented Apr 21, 2016 at 11:47
  • Please let me think what to do Commented Apr 21, 2016 at 12:24

1 Answer 1

1
 /**
*Get profile_pic*/
public function callmethod($userId){
$stmt = $this->conn->prepare("SELECT profile_pic FROM users WHERE unique_id=?");
$stmt->bind_param('s',$userId); 
//$result = mysql_query($query) or die(mysql_error()); 
//$photo = mysql_fetch_array($result); 
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($profile_pic);
while ($stmt->fetch()) {
    echo "<img src='data:image/jpeg;base64,".$profile_pic."' />";
}
 //$obj->picture = base64_encode($profile_pic);
//echo $obj;


}

ok try this code this.you don't need header("Content-Type: image/jpeg"); function. this are error in your php code. this code will create img tag with basc64.

now for android part.

change this in php.

while ($stmt->fetch()) {
    echo "<img src='data:image/jpeg;base64,".$profile_pic."' />";
}

to

while ($stmt->fetch()) {
    echo $profile_pic;
}

and this would be your android part.

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried out the above code, it gave me the result for the profile_pic variable but when I try it out with the image tag it still shows nothing, I just get an icon with broken image.
you are already saving image in base64 format right? i completely missed that. so in php you don't need base64_encode() function again, so i am updating my code, correct me if i am wrong.
Unfortunately the decoding in the android development part not showing the image
there must be error in logcat what it is..can your share?
I have got it worked out.. I was storing only strings in my SQLite DB instead of blob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.