Image Doesn't Show in my Android ImageView - android

I am creating android web application using RESTFul web services.i have declared code to save Image like this
onCreateMethod()
ImageView iv = (ImageView) findViewById(R.id.hotelLogoImageView);
iv.setDrawingCacheEnabled(true);
onSave()
Bitmap b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
logo = bos.toByteArray();
then i am using RESTFul webservices.so i converted logo(byte Array) to String and passed logo into NameValuePair like:
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("hotel_logo",String.valueOf(logo)));
In my server side code, again i converted my String value to byteArray like:
byte[] imgLogo = hotelParams.getFirst("hotel_logo").getBytes(
Charset.forName("UTF-8"));
Finally i saved imgLogo value in my phpMyAdmin Database.it's also Stored in Database like [BLOB - 10 B]
Now I have to set that image into ImageView.so I have used this method.like
JSONObject jsonObject = new JSONObject(response);
byte[] logo = Base64.decode(jsonObject.getString("hotel_logo"),
Base64.DEFAULT);
Bitmap hotel_Logo = BitmapFactory.decodeByteArray(logo, 0, logo.length);
hotelLogo.setImageBitmap(hotel_Logo);
After finish this code.there is no error.But Image Doesn't Show in ImageView.please Help me what i have done mistake?

Related

loopj - Android Asynchronous Http Client upload image from drawable

I'm trying to send an image from drawable to rest web service use android asynchronous http client. This link show how to send image use path,
Uploading an image from Android (with Android Asynchronous Http Client) to rails server (with paperclip)
Is it possible to send image from drawable? I have already look from Android Asynchronous Http Client about how to send byte image, but in my code it's keep return error that the picture is missing. Here is my code
RequestParams params = new RequestParams();
username = editTextEmail.getText().toString();
name = editTextName.getText().toString();
password = editTextPassword.getText().toString();
phone = editTextPhone.getText().toString();
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_user);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte = stream.toByteArray();
if(isOnline()){
params.put("email", username);
params.put("name", name);
params.put("password", password);
params.put("phone", phone);
params.put("picture", new ByteArrayInputStream(imageInByte), "user.jpg");
invokeWS(params);
return true;
}
Can anyone help me? Thanks before
Yes, it's possible to send image from drawable, but you should convert your image to base64 string like this :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
and then send the string to the server :
params.put("image", image_str);
For more details look here Android application to send image to MySQL

how to upload image as byte array to Rest web-service using http post method in android

I created app which can upload the images to the web-service. In that, how can I upload use the image as byte[] array while uploading image using HTTP post method to rest web-service.
How to pass the byte[] array to web-service along with file name and file length?
Drawable d = context.getResources().getDrawable(R.drawable.human_head);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] bitmapdata = stream.toByteArray();
entity.addPart("image", new ByteArrayBody(bitmapdata,".png"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, WebService.getInstance(Constants.WEB_SERVER).getLocalContext());

Insert Image to mysql database in android using PHP

I have a problem to save a image to mysql using PHP.
I have some code but it's got some errors.
Here is my code.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.img_upload);
//img_Photo.setImageBitmap(bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
image_str= Base64.encodeToString(byte_arr, 1);
postParameters.add(new BasicNameValuePair("photo", image_str));
Here, bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); is always got Null.
My code is wrong or which part should i repair. Give me some advices.
Change your line as below:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img_upload);
If you want to get the image from ImageButton try out as below:
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
first convert your image into a base 64 byte array encoded string. after that send it to php. extract on server side.Then store that string in MySQL. after that send that string to android client. extract image string and decode with base 64 decode. after that you will get byte array you can simply show in your image view. for your reference I will show some code
String imagedata = Base64.encodeToString(thumbnailArray,Base64.DEFAULT);
mJobject.put("imagebyte",imagedata);
mJArray.put(mJobject);
JSONArray json=new JSONArray(response);
JSONObject jo = null;
imageArray=new String[json.length()];
imageArray[i]=jo.getString("imageid");
completeImage= Base64.decode(imageArray[0],Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(completeImage , 0, completeImage.length);

Can we send image as a string parameter in webserrvices in android

I need to send image as a string parameter in webservices to server. Just like we send string parameter in webservices. For this I am converting image into string as follows
Resources r = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.icon);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); //bm is the bitmap object
byte[] image = baos.toByteArray();
String encodedImage = Base64.encodeToString(image,Base64.DEFAULT);
Now I need to send this string encodedImage like this.
calling Url-- http://pdtrptr/asfsdf/services/add.php?file=encodedImage
My question is how can we send this string image along with url --like above url
Is server coding for receiving string image differs for android compare to iphone because (after sending the image from android to server, when trying to get from server i am getting null value), where as iphone mobile is able to send and receive the image.
Thanks
We can try the code which is providing in following link...
Image Upload with HttpPost

Dynamically conversion of image to binary and vice versa

How can I convert image to binary data..???
I want to send that converted binary data to
another device or to the web server.
Which mechanism is best to do this.?
Image is in Bitmap then use the following code to convert that image to binary. By using following code
Bitmap photo;// this is your image.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
To get Image From Binary use the following sample:
Bitmap bMap = null;
bMap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
I found a good example for uploading the image to the server.
create a bitmap variable before do anything.
variable to set a name to the image into SD card.
this variable, you have to put the path for the File, It's up to you.
sendData is the function name, to call it, you can use something like
sendData(null).
remember to wrap it into a try catch.
private Bitmap bitmap;
public static String exsistingFileName = "";
public void sendData(String[] args) throws Exception {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
// here, change it to your php;
HttpPost httpPost = new HttpPost("http://www.myURL.com/myPHP.php");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
bitmap = BitmapFactory.decodeFile(exsistingFileName);
// you can change the format of you image compressed for what do you want;
// now it is set up to 640 x 480;
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or whatever you want;
bmpCompressed.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
// sending a String param;
entity.addPart("myParam", new StringBody("my value"));
// sending a Image;
// note here, that you can send more than one image, just add another param, same rule to the String;
entity.addPart("myImage", new ByteArrayBody(data, "temp.jpg"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
} catch (Exception e) {
Log.v("myApp", "Some error came up");
}
}
Try this
Let img contains Bitmap image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
imageInByte now contains bytedata of bitmap image.
For converting reverse
Bitmap bp = BitmapFactory.decodeByteArray(imgArray, 0,imgArray.length);
Hope this may help you
if you want to send to webserver use HttpPost request using HttpClient

Categories

Resources