39725

How to upload long blob (image) to mysql database using java and retrieve in php?

Question:

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

Answer1:

/** *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);

Recommend

  • AES-CTR Encrypt in CryptoJS and decrypt in Go lang
  • Is it a good idea to store bitmap as BLOB in Sqlite?
  • FileStream is not marked as serializable, but the whole class is?
  • What audio format Spotify uses and how they transfer the files to mobile devices?
  • Attaching an image to an email
  • catching unknown host exception in codename one
  • Convert list of N items to relative ordering (0-(N-1))? [duplicate]
  • How enable GZIP compression for Spring HttpInvoker requests using Tomcat?
  • Migrate java code from hbase 0.92 to 0.98.0-hadoop2
  • JPEGEncoderOptions is undefined
  • extjs, is it posible to compres load ext-all.js?
  • Convert VarBinary RTF blob to text in MS SQL
  • excluding some folders in tar command not work
  • How do int-to-string casts work in Go?
  • How to force Hadoop to unzip inputs regadless of their extension?
  • Random(staticSeed).Next() alternative that will never change implementation and is guaranteed consis
  • How to keep image quality same in BitmapFactory
  • Embed multiple jpeg images into EXCEL programmatically?
  • How do I deploy my Node.js app with a opencv4nodejs dependency to Heroku?
  • How to autoplay video in angularjs?
  • SOAP with Attachment / MIME content
  • Index.php as custom error page
  • How to get the url of a file of google drive to download in android
  • Insert records if not exist SQL Server 2005
  • Convert RSA pem key String to der byte[]
  • Negating Regex PO BOX
  • Serve file to user over http via php
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Why does it draw lines in the wrong place?
  • Floated image with variable width and heading with background image
  • How to have background script and something similar to a default popup?
  • Python urlparse: small issue
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • Rearranging Cells in UITableView Bug & Saving Changes
  • GridView Sorting works once only
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check