Can we send image as a string parameter in webserrvices in android - 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

Related

How to send class which contain Image from Android to Java Restful Web server using Retrofit

I use Retrofit to transfer Data(class) from android to Server.Generally we create Same class on Client and Server side which contain data type like int,String same on both side.Now I need to send Image in class to Server.Also I just don't want to send just image to server but I want to send class which contain 1 image as datatype.So how can I do that ?or any suggestion how can I do this with some other tool ?
Suppose you already have a File pointing to the image:
final File imageFile = ...;
Note: How to get this file depends on whether you only allow local files in your file chooser (or e. g. images that are actually in Google Drive) and on which Android version you are on, etc.
To upload the image you need to use Retrofit's TypedFile - here is what worked for me:
#POST("/api/image/upload")
#Multipart
public void submitPictureToVoting(#Part("user_id") Integer userId,
#Part("image_title") String imageTitle,
#Part("file") TypedFile file,
Callback<UploadImageResponse> callback);
And then:
final TypedFile typedImageFile = new TypedFile("application/octet-stream", imageFile);
mApiClient.submitPictureToVoting(
1234567,
"This is me!",
typedImageFile,
new Callback<UploadImageResponse> {
// ...
});
I dont think this is answer but by doing this we can solve proble of Retrofit easily
Hi I tried this by Encoding Image to String ,so that we can easily send this "String" data type like other String, & when we want image back,we can decode it.
1.Code for Encoding
Here Image taken from ImageView.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
2.Code for Decoding
byte[] decodedByte = Base64.decode(encodedImage, 0);
Bitmap decodedImg= BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
imageView.setImageBitmap(decodedImg);

Encoding images for URI use

I am busy with a small android app. The appplication connects to a WCF using restful that in turn connects to a database. The application does work. Now I want to send a Image to the database.
[OperationContract]
[WebGet(UriTemplate = "sendImage/{seq}", ResponseFormat = WebMessageFormat.Json)]
string sendImage(string seq);
On the android application I encode a Image making use of BASE64.
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
TextView textView = (TextView) findViewById(R.id.defaults);
textView.setText(encodedImage);
This encryption does work. The problem is that I now what to send the string encodedImage to the WCF to write to the database. Thus the string is passed in the URI.
"htp://_____.__/serv/ManagementSystem.svc/sendImage/" + encodedImage
The problem is that the string that I get from encoded contains / characters.
/9J/DASDFkFASDF/.....
This causes a problem in the URI as it picks up the / as a change of parameter, thus picking up that it is not a valid request. Is there a way to remove the / without compromising the decoding or is there a better way of converting a image to a string and back to an image?
If you have control over the server side, I can at least think of two options:
1) After base64 encoding, replace all / with _, which is valid for url, has no particular pre-assigned meaning, and it is not used by base64. On the server side, you apply the opposite replace;
2) This is more straightforward that option 1, instead of get, you use put or post.

why do i get wrong base 64 string in the code below?

I have the code below to decode a bitmap to a base64 string.
for(String e:paths)
{
String usepath=e.replace("%", "//");
Bitmap m=BitmapFactory.decodeFile(usepath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String bb= Base64.encodeToString(b, Base64.NO_WRAP);
Log.e("Photo", bb);
String usepath prints like
/mnt/sdcard/DCIM/Camera/IMG_20140424_132023.jpg
I have saved the image on my pc and used an online tool to decode it to base64 and i got a long string of around 650kb(after uploading to google app engine) yet the string i get using the above code is like 10% of that and does not display the image .
But i can use the same image path to set an image view and it works like below
Bitmap bm= BitmapFactory.decodeFile(usepath);
holder.imageItem.setImageBitmap(bm);
Any reasons why the base64 encoding failing?
Ronald
Try adding the flag Base64.URL_SAFE to the encoding method.
Consider also that if the image is too large you may not get all the bytes you need in the String (you may try writing to a temp file previous to send the content).

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

How to send uiimage to another device through json data or string?

I am new to iPhone development.Currently I am working on an application in which I need to send an image as well as text. So I thought of using json technology by sending the string as json data. Now I need to append this image to this string. Can anyone suggest a method to do this?
This application should also be able to bump with an android phone. Is there any method to do this?
I heard of converting the image to base64 and send as string. Is that the right method to do it?
converting image to base64 is the right method for this. please have look at following code snippet
public String convertToBase64(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] image = baos.toByteArray();
String encodedImage = Base64.encodeToString(image, Base64.DEFAULT);
return encodedImage;
}
You can use this string to send through JSON data
Edited Part
For iPhone Try this code
-(NSString *)getStringFromImage:(UIImage *)image{
if(image){
NSData *dataObj = UIImagePNGRepresentation(image);
return [dataObj base64Encoding];
} else {
return #"";
}
}
Hope this will solve your problem

Categories

Resources