Reputation: 1318
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)
Upvotes: 2
Views: 10276
Reputation: 21
<?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'];
?>
Upvotes: 1
Reputation: 11744
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
Upvotes: 4