7 Replies - 5022 Views - Last Post: 22 May 2012 - 06:18 PM

#1 jersam515  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 12

How can I send an image from an sql database to an android client?

Posted 21 May 2012 - 02:51 PM

I'm trying to send an image from an sql database to an android client. I'm doing this by way of a php middleman. The code stops working due to a null returning of the method below. My php code(works fine to encode image into base64(decoded the encoding and it worked)):

<?php
    //get image
    function ob_base64_encode($c) {
    $a=base64_encode($c);
    $arr = array('base64' => $a);
      return json_encode($arr);
    }
    $image=resize(48,80,$image);
    // And pass its name as a string
    ob_start('ob_base64_encode');
    imagepng($image);
    ob_end_flush();
?>

My android code:

public static Bitmap getIm(String IP, int height, int width)
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//Network queries and gets response(works)
    String result = Network.getResponse(IP, nameValuePairs);
//manually decode string since for some reason JSON decode won't work(also works)
String r=result.split(":\"")[1];
r=r.split("\"")[0];
//decode string
    byte[] decodedString = Base64.decode(r, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    return decodedByte;
//returns null
    }


Is This A Good Question/Topic? 0
  • +

Replies To: How can I send an image from an sql database to an android client?

#2 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 06:18 AM

Why do you storage the images in the database?
That will just make it slower and bigger.
I would save the images to a folder in the server, and the save the path as string, in the database.
Like:

From database: "\images\img_001.jpg"
Usage:
String image = *the string from the database*;
Thread t = new Thread() {
@Override
public void run() {
try {
loadBitmap(image);
} catch (Exception e) {
Log.e("DICActivity", e.getMessage();
}
}
};
t.start();



And to load the image, we use a lazyload.
That way, the app wont stop responding until the image is loaded.
public void loadBitmap(String url) {
		
	    Bitmap bitmap = null;
	    InputStream in = null;
	    BufferedOutputStream out = null;

	    try {
	    	if (url != null && url != "") {
	        in = new BufferedInputStream(new URL(url).openStream());

	        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
	        out = new BufferedOutputStream(dataStream);
	        int byte_;
	        while ((byte_ = in.read()) != -1){
	          out.write(byte_);
	      }
	        out.flush();

	        final byte[] data = dataStream.toByteArray();
	        BitmapFactory.Options options = new BitmapFactory.Options();
	        //options.inSampleSize = 1;

	        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
	        dataStream.close();
			in.close();
			out.close();
	    	} else {
	    		url = "http://cdn3.dreamincode.net/dreamincode/home/images/logo_2012.gif,qv=3.pagespeed.ce.p51tf8xZ5T.gif";
	    		try {
	    		        in = new BufferedInputStream(new URL(url).openStream());

	    		        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
	    		        out = new BufferedOutputStream(dataStream);
	    		        int byte_;
	    		        while ((byte_ = in.read()) != -1){
	    		          out.write(byte_);
	    		      }
	    		        out.flush();

	    		        final byte[] data = dataStream.toByteArray();
	    		        BitmapFactory.Options options = new BitmapFactory.Options();
	    		        //options.inSampleSize = 1;

	    		        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
	    		        dataStream.close();
	    				in.close();
	    				out.close();
	    			
	    		} catch (Exception e) {
	    			Log.e("DICActivity", e.getMessage());
	    		}
	    		
	    	}
	    } catch (Exception e) {
	        Log.e("DICActivity", "Could not load Bitmap from: " + url);
	    }    
	    final Bitmap bitimg = bitmap;
            handler.post(new Runnable() {
            public void run() {
                     *your imageView*.setImageBitmap(bitimg);
                }
            });
	   
	}


Hope this helps.

Cheers

This post has been edited by EndLessMind: 22 May 2012 - 06:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 jersam515  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 12

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 04:47 PM

Thanks for taking a look, unfortunately your method returns the same error of a null bitmap even when using this : http://cdn3.dreaminc.../logo_2012.gif.
Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 05:34 PM

Okey, what dose the logcat say?

I'm not use if it can load gif-images.
Try a link like this one:
http://www.androidde...alanumber_1.png
Was This Post Helpful? 1
  • +
  • -

#5 jersam515  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 12

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 05:44 PM

The error just comes from a nullpointer when I attempt to display the image. In an image view. The bitmap comes out to be a null bitmap. The same issue arises with the last image.
Was This Post Helpful? 0
  • +
  • -

#6 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 05:56 PM

Please post your exact code :)
Was This Post Helpful? 1
  • +
  • -

#7 jersam515  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 12

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 06:08 PM

I am extremely sorry, as soon as you asked about that I realized my mistake. I will upvote all your posts for the help.
Was This Post Helpful? 0
  • +
  • -

#8 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: How can I send an image from an sql database to an android client?

Posted 22 May 2012 - 06:18 PM

Okey :)
Good job on solving it :)
That the think that i like best with helping people, when I give them some help and they can work it out them self. That's prof that it's not all copy & past-programmers in the world :)

I wish you good luck with your project :)

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1