2

I have an image stored as BLOB in my MySQL database. I am returning the image back to the android app via PHP JSON array like this: $row_array['image'] = base64_encode($row['image']); Now I am getting the value in android like this String image = c.getString(TAG_IMAGE); where c is a JSON Object. Then I tried decoding it

byte[] b = Base64.decode(image, 0);

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);

But this just gives me a null pointer exception and crashes. What am I doing wrong here?

Logcat:

05-16 15:40:47.469: E/AndroidRuntime(30925): FATAL EXCEPTION: main
05-16 15:40:47.469: E/AndroidRuntime(30925): java.lang.NullPointerException
05-16 15:40:47.469: E/AndroidRuntime(30925):    at com.example.shareity.ListNew$JSONParse.onPostExecute(ListNew.java:254)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at com.example.shareity.ListNew$JSONParse.onPostExecute(ListNew.java:1)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at android.os.AsyncTask.finish(AsyncTask.java:631)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at android.os.Looper.loop(Looper.java:137)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at android.app.ActivityThread.main(ActivityThread.java:4745)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at java.lang.reflect.Method.invokeNative(Native Method)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at java.lang.reflect.Method.invoke(Method.java:511)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-16 15:40:47.469: E/AndroidRuntime(30925):    at dalvik.system.NativeStart.main(Native Method)
5
  • 1
    Post the exception. Is it on Base64.decode (there is no data in image) or in BitmapFactory.decodeByteArray (the data could not be properly base64 decoded)? Commented May 16, 2015 at 15:42
  • It just says NullPointerException as blah blah @JPMoresmau Commented May 16, 2015 at 15:45
  • @YohanBlake it might not look like anything more than "blah blah" to you, but your answer might be inside that "blah blah". If you post the exception output, someone might be able to help you find an answer from it. Commented May 16, 2015 at 16:15
  • @DanGetz I have added the logcat Commented May 17, 2015 at 6:05
  • Ok, now you can see in the 3rd line of the logcat, that it crashes when, on line 254 of ListNew.java, you try to use a value which is null. That might be a clue as to what's going on. Is that line one of the lines of code you already posted? Commented May 17, 2015 at 12:16

2 Answers 2

4

Suppose the event is the table name , image is the column name that holds the BLOB Image and user_id is the id of the user/row . To extract the image from the BLOB, You should create an EXTRA PHP File. which would like something like this.

get_image.php

<?php 
$db = mysql_connect("localhost","user","password") or die(mysql_error()); 
mysql_select_db("shareity",$db) or die(mysql_error()); 
$userId = $_GET['eid']; 
$query = "SELECT image FROM event WHERE eid='$userId'"; 
$result = mysql_query($query) or die(mysql_error()); 
$photo = mysql_fetch_array($result); 
header('Content-Type:image/jpeg'); 
echo $photo['image']; 
?>

Now,get_image.php is like an image. if you pass a user_id through GET to the get_image.php will throw the user_image.

So when ever you encode the json from server side,add a field which append the user id with the get_image.php like this

{
 'ID':1,
 'userName':'John',
 'image_url':'http://your-host-name/get_image.php?ID=1'
}

So from the Android, you can decode the image like this

try {
    URL url = new URL("http://your-host-name/get_image.php?ID=1");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
    // Log exception
    return null;
}

More clarification can be fnd here

Sign up to request clarification or add additional context in comments.

5 Comments

can you please tell me what to include in my original php file in order to get the JSON result { "image_url":"http://example.com/myImageDecoder.php?id=1" }?
it has to be $row_array['image'] = something ; Can you please tell me what to add there?
Yeah, $con holds the db connection.Let me ask you some question. what is the table name and column name which holds the image as BLOB data-type ?
Table name is event. Column name is image
1
<?php 
$db = mysql_connect("localhost","user","password") or die(mysql_error()); 
mysql_select_db("shareity",$db) or die(mysql_error()); 
$userId = $_GET['eid']; 
$query = "SELECT image FROM event WHERE eid='$userId'"; 
$result = mysql_query($query) or die(mysql_error()); 
$photo = mysql_fetch_array($result); 
header('Content-Type:image/jpeg'); 
echo $photo['image']; 
?>

Comments

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.