7

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

23
  • Help Me friend i need your help....... Commented Mar 27, 2015 at 14:29
  • t gives me result like . What are you printing there? Which result? At least put the log statement in your code so we can see what you are printing. Commented Mar 27, 2015 at 14:46
  • 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) Commented Mar 27, 2015 at 14:48
  • You had already posted that. Why are you posting that again? You did not understand my question. I quoted you. Look in your text for the origin of the quote. Then add the asked code in the post. Commented Mar 27, 2015 at 14:53
  • About your null pointer exception. Tell which pointer is null. Commented Mar 27, 2015 at 14:54

5 Answers 5

1

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

Picasso.with(getActivity()).load(url).into(imageView);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.