通过JSON从mysql数据库中获取BLOB映像

时间:2015-05-16 15:38:26

标签: php android mysql json blob

我在MySQL数据库中存储了一个BLOB图像。我通过PHP JSON数组将图像返回到Android应用程序,如下所示:$row_array['image'] = base64_encode($row['image']); 现在我在android中获得了这个String image = c.getString(TAG_IMAGE);的值,其中c是一个JSON对象。然后我尝试解码它

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

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

但这只是给我一个空指针异常和崩溃。我在这里做错了什么?

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)

2 个答案:

答案 0 :(得分:3)

假设event是表名,image是包含BLOB图像的列名,user_id是用户/行的ID。要从BLOB中提取图像,您应该创建一个EXTRA PHP文件。想要这样的东西。

<强> 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']; 
?>

现在,get_image.php就像一张图片。如果您将user_id通过GET传递给get_image.php,则会抛出user_image

因此,当您从服务器端对json进行编码时,添加一个用get_image.php添加用户ID的字段

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

因此,从Android中,您可以像这样解码图像

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;
}

更多说明可以是fnd here

答案 1 :(得分: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']; 
?>