Display Image From Mysql DataBase

Put your problem here if it does not fit any of the other categories.

Display Image From Mysql DataBase

Postby mo5ito » Thu Mar 17, 2011 8:53 pm

hi
i upload an image on mysql dataBase as Blob Format ,but when i run my Json file to get the data
here is the result : [{"Name":"Jack","Photo":null}]
Json dosen't support binary data so how am i going to do ?

i check some forums,and i found that i have transform the photos to a string with Base64 and then as usual i can get the String with json_data.getString("photo") .

So how Convert the photo(Blob) to a String (base64) ?

thx :D
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Top

Re: Display Image From Mysql DataBase

Postby raj_J2ME » Fri Mar 18, 2011 6:03 am

I think converting the Image directly to the Base64 represented string,

You can use the Base64 Android class:
Code: Select all
String encodedImage = Base64.encode(byteArrayImage, Base64.DEFAULT);

You'll have to convert your image into a byte array though. Here's an example:

Code: Select all
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

* Update *

If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 aka version 2.2).

Is this serves your aim?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Display Image From Mysql DataBase

Postby mo5ito » Fri Mar 18, 2011 9:22 am

first,when i'm trying to return the image into a binary format ,i got : [{"Name":"Jack","Photo":null}]
how can i convert the image from binary format to Base64 with:
String encodedImage = Base64.encode(byteArrayImage, Base64.DEFAULT);
when i'm getting NULL at the Photo Field

thx for your help
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Re: Display Image From Mysql DataBase

Postby mo5ito » Fri Mar 18, 2011 9:47 pm

<?php
mysql_connect("localhost","root","root");
mysql_select_db("mvc");
$q=mysql_query("SELECT photo FROM profil ");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));

mysql_close();
?>
i got to encode it in base64, with base64_encode() but how ?
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Re: Display Image From Mysql DataBase

Postby mrfalco » Fri Mar 25, 2011 10:31 am

hi i have your similar problem bat i think for php encode to help you try this:

<?php
mysql_connect("localhost","root","root");
mysql_select_db("mvc");
$q=mysql_query("SELECT photo FROM profil ");
while($row=mysql_fetch_assoc($q)) {
$array=$row;
$array['photo']=base64_encode($row['photo']);
$output[]=$array;
}
print(json_encode($output));

mysql_close();
?>

I will hope to help you
mrfalco
Freshman
Freshman
 
Posts: 2
Joined: Fri Mar 25, 2011 9:38 am

Re: Display Image From Mysql DataBase

Postby mo5ito » Sat Mar 26, 2011 11:22 am

Code: Select all
      <?php
      mysql_connect("localhost","root","root");
      mysql_select_db("mvc");
      $q=mysql_query("SELECT * FROM connexion ");
      while($row=mysql_fetch_assoc($q))
   $array=$row;
   $array['photo']=base64_encode($row['photo']);
   $output[]=$array;
      print(json_encode($output));
      mysql_close();
      ?>

in the database:
Image
but nothing in the Json output :
Image
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Top

Re: Display Image From Mysql DataBase

Postby mo5ito » Sun Mar 27, 2011 12:57 pm

<?php
mysql_connect("localhost","root","root");
mysql_select_db("mvc");
$q=mysql_query("SELECT * FROM connexion ");
while($row=mysql_fetch_assoc($q))
{
$array=$row;
$array['photo']=base64_encode($row['photo']);
$output[]=$array;
}
print(json_encode($output));
mysql_close();
?>

then you have an output base64 String
now in your IDE you have to decode that base64 string to Byte Format to finaly display it in an image view

byte[] bit = Base64.decode(json_data.getString("photo"),0);
ImageView image = (ImageView)convertView.findViewById(R.id.widget34);
image.setImageBitmap(BitmapFactory.decodeByteArray(bit, 0, bit.length));

8)
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Re: Display Image From Mysql DataBase

Postby n3wbie » Fri Apr 08, 2011 11:46 am

woulda you like to share your java code here please
n3wbie
Freshman
Freshman
 
Posts: 3
Joined: Sun Mar 27, 2011 5:20 pm

Re: Display Image From Mysql DataBase

Postby bumdeal2 » Tue Apr 12, 2011 12:30 pm

Hey mo5ito,

Just out of interest how is your widget34 declared in your xml file.
bumdeal2
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 09, 2011 11:05 pm

Re: Display Image From Mysql DataBase

Postby n3wbie » Tue Apr 19, 2011 5:18 pm

can i see your java code for this problem please.....
n3wbie
Freshman
Freshman
 
Posts: 3
Joined: Sun Mar 27, 2011 5:20 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 8 guests