【问题标题】:Retrieve image from mysql-php(android)从 mysql-php(android) 检索图像
【发布时间】:2015-05-31 22:00:11
【问题描述】:

我在图像表字段中有一张图像,我想在我的应用程序中使用该图像 我的 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)

如何解决那个空指针异常...当我收到图像字符串时.....

【问题讨论】:

  • 帮帮我的朋友我需要你的帮助.......
  • t gives me result like 。你在那里打印什么?哪个结果?至少将日志语句放入您的代码中,以便我们可以看到您正在打印的内容。
  • 03-27 10:10:44.355: E/AndroidRuntime(2391): java.lang.NullPointerException: 输入字符串为空。 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)
  • 你已经发布了。你怎么又发这个了?你没有理解我的问题。我引用了你。在您的文本中查找引用的来源。然后在帖子中添加询问的代码。
  • 关于你的空指针异常。告诉哪个指针是空的。

标签: php android mysql android-imageview android-bitmap


【解决方案1】:

您可以使用 Picasso 轻松加载图像。例如:

Picasso.with(getActivity()).load(url).into(imageView);

【讨论】:

    【解决方案2】:

    检查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 中的文档链接。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        可能是由于用于 php 和 android 中的编码和解码的字符集引起的问题

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

        参考此链接解决您的问题 https://stackoverflow.com/a/15156991/4985541

        【讨论】:

          【解决方案5】:

          如果你得到的是 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);
          }
          

          【讨论】:

            猜你喜欢
            • 2019-09-22
            • 2011-08-25
            • 1970-01-01
            • 2014-02-19
            • 2015-07-06
            • 1970-01-01
            • 2016-01-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多