Tufan
Tufan

Reputation: 2839

Retrieve image from mysql-php(android)

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

$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

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.....

Upvotes: 7

Views: 1757

Answers (5)

Vid
Vid

Reputation: 1012

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

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

Upvotes: 0

NullByte
NullByte

Reputation: 165

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

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

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

Upvotes: 0

Shoeb Siddique
Shoeb Siddique

Reputation: 2825

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

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

Upvotes: 0

Shvet
Shvet

Reputation: 1201

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.

Upvotes: 1

parik dhakan
parik dhakan

Reputation: 787

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

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

Upvotes: 1

Related Questions