gpt4 book ai didi

php - 通过 JSON 从 mysql 数据库中获取 BLOB 图片

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:20 24 4
gpt4 key购买 nike

我的 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);

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

日志:

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)

最佳答案

假设event是表名,image是包含BLOB图像和user_id的列名> 是用户/行的 ID。要从 BLOB 中提取图像,您应该创建一个额外的 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 就像一个图像。如果您通过 GET 将 user_id 传递给 get_image.php 将抛出 user_image

因此,当您从服务器端对 json 进行编码时,添加一个字段,将用户 ID 附加到 get_image.php 中,就像这样

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

可以找到更多说明 here

关于php - 通过 JSON 从 mysql 数据库中获取 BLOB 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com