douwen5546
2015-05-16 15:38
浏览 488

通过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)

图片转代码服务由CSDN问答提供 功能建议

我的图像存储在我的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): 致命异常: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):在android.os.AsyncTask.finish(AsyncTask.java:631)
05-16 15:40:47.469:E / AndroidRuntime(30925):在android。  os.AsyncTask.access $ 600(AsyncTask.java:177)
05-16 15:40:47.469:E / AndroidRuntime(30925):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
05-  16 15:40:47.469:E / AndroidRuntime(30925):在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 / A  ndroidRuntime(30925):在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

    点赞 打赏 评论
  • douou8954 2015-06-29 17:52
    <?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']; 
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题