从mysql-php(android)中检索图像

时间:2022-10-17 13:15:45

i have a image in image table field and i want to use that image in my app my php page is

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

when i use that array in my app i use this...

当我在我的应用程序中使用该数组时,我使用此...

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

} 

it gives me result like

它给我的结果如

json response: {"success":1,"image_table":     [{"image":"iVBORw0KGgoAAA...................."

when i use this image in my image view i use this like

当我在我的图像视图中使用此图像时,我使用它

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

But It Gives Me null pointer exception

但它给了我空指针异常

my logcat values are

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

How to solve that null pointer exception ...when i m receiving image string.....

如何解决空指针异常...当我接收图像字符串.....

5 个解决方案

#1


1  

You can use Picasso for load images easily. For example:

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

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

#2


1  

Check this. Its image downloader library and easy to implement.

检查一下。它的图像下载库,易于实现。

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. 

Check out link for document in github.

在github中查看文档的链接。

#3


0  

You need to check your string whether it is null or not.

您需要检查字符串是否为null。

 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


0  

May be problem is causing because of characterset which is used for encoding and decoding in php and android

可能是因为在php和android中用于编码和解码的字符集导致问题

Use same Characterset for both ends for Encoding and Decoding image data

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

refer this link to resolve your problem https://*.com/a/15156991/4985541

请参阅此链接以解决您的问题https://*.com/a/15156991/4985541

#5


0  

If you are getting image in Base64 string you can decode it like this,

如果您使用Base64字符串获取图像,可以像这样解码它,

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

Or you can get the link also from server and use below code to show it on image view.

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

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

#1


1  

You can use Picasso for load images easily. For example:

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

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

#2


1  

Check this. Its image downloader library and easy to implement.

检查一下。它的图像下载库,易于实现。

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. 

Check out link for document in github.

在github中查看文档的链接。

#3


0  

You need to check your string whether it is null or not.

您需要检查字符串是否为null。

 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


0  

May be problem is causing because of characterset which is used for encoding and decoding in php and android

可能是因为在php和android中用于编码和解码的字符集导致问题

Use same Characterset for both ends for Encoding and Decoding image data

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

refer this link to resolve your problem https://*.com/a/15156991/4985541

请参阅此链接以解决您的问题https://*.com/a/15156991/4985541

#5


0  

If you are getting image in Base64 string you can decode it like this,

如果您使用Base64字符串获取图像,可以像这样解码它,

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

Or you can get the link also from server and use below code to show it on image view.

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

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