从MySQL-PHP(Android)检索图像

7

我有一个存储在图像表字段中的图片,我想在我的应用程序中使用这张图片。 我的PHP页面是:

$user = array();
$user["image"] = base64_encode($result["image"]);
// success
$response["success"] = 1;

// user node
$response["image_table"] = array();

array_push($response["image_table"], $user);

当我在我的应用程序中使用这个数组时,我使用以下代码...
if (success == 1)
{
    address = json.getJSONArray(TAG_IMAGE_TABLE);
    for (int i = 0; i < address.length(); i++) {
    JSONObject c = address.getJSONObject(i);
    image = c.getString(TAG_IMAGE);

} 

它会给我返回结果,例如:
json response: {"success":1,"image_table":     [{"image":"iVBORw0KGgoAAA...................."

当我在我的图像视图中使用这个图像时,我会像这样使用它:

ImageView ivProperty = ((ImageView) myContentView.findViewById(R.id.image_property));

byte[] decodedString;
try {
        decodedString = Base64.decode(image, Base64.URL_SAFE);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ivProperty.setImageBitmap(decodedByte);
    } catch (IOException e) {
// TODO Auto-generated catch block
        e.printStackTrace();
    }

但是它给了我空指针异常

我的logcat值为

03-27 10:10:44.355: E/AndroidRuntime(2391):   java.lang.NullPointerException: Input string was null.
03-27 10:10:44.355: E/AndroidRuntime(2391):     at com.big_property_info.Base64.decode(Base64.java:1242)
03-27 10:10:44.355: E/AndroidRuntime(2391):     at  com.big_property_info.MainActivityMap$GeocoderTask$2.getInfoContents(MainActivityMap.java:314)

如何解决接收图像字符串时出现的空指针异常...

“image”是从哪里来的 - 我没有看到分配它的代码。JSON将“image”嵌入在一个数组中的关联数组中,而不是一个变量中。 - Rick James
  1. 使用JSON库解析您的JSON响应,创建JSON对象并打印您的Base64图像字符串。如果打印成功,则将其传递给Base64。
  2. Base64不是下载或上传图像的好主意,因为创建Base64会使大小增加3倍,Android应用程序可能会超出堆限制。
- Ketan Parmar
为什么MySQL被标记了? - Rick James
Base64.decode()这部分是否在同一个if(success == 1)代码块中运行? - Leo supports Monica Cellio
谷歌介绍了 Volley 库,它非常有助于您加载图像。 - nifCody
显示剩余18条评论
5个回答

1
你可以使用Picasso轻松地加载图片。例如:
Picasso.with(getActivity()).load(url).into(imageView);

1

请查看 this。它的图片下载器库易于实现。

DisplayImageOptions imageOptions;
ImageLoader imageLoader; 

     imageOptions = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable
                    .logo_image).showImageOnFail(R.drawable.logo_image).cacheInMemory(true)
                    .cacheOnDisk(true)
                    .build();
            imageLoader = ImageLoader.getInstance();
            imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));

imageLoader.displayImage(uri, imageView, imageOptions);
//Where uri is url of imageview stored in server. imageview is Imageview in which you want to show image. 

在github上查看文档链接。


0

你需要检查你的字符串是否为空。

 ImageView ivProperty = ((ImageView) myContentView.findViewById(R.id.image_property));

    byte[] decodedString;
    try {   
if (image!=null&&!image.equalsIgnoreCase("")) {
               decodedString = Base64.decode(image, Base64.URL_SAFE);
                  Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                  ivProperty.setImageBitmap(decodedByte);
        }
        } catch (IOException e) {
    // TODO Auto-generated catch block
            e.printStackTrace();
        }

0

如果您得到的图像是Base64字符串,您可以按如下方式对其进行解码:

byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");

或者您也可以从服务器获取链接,并使用以下代码在图像视图中显示它。

if (imageUrl != null && isImageUrl) {
    Picasso.with(getApplicationContext()).load(Constants.IMG_URL + imageUrl).resize(150, 100).centerInside().into(ivActionHome);
}

0

可能问题是由于在 PHP 和 Android 中用于编码和解码的字符集不同导致的

请在编码和解码图像数据时同时使用相同的字符集

请参考此链接以解决您的问题 https://dev59.com/KGUp5IYBdhLWcg3woonS#15156991


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接