douwen5546 2015-05-16 15:38
浏览 746
已采纳

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

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)
  • 写回答

2条回答 默认 最新

  • dsux90368 2015-05-16 15:40
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)