I am using params.put to post the strings to the server. But how do I post an image which is saved in the variable imgPreview.
comp_logo_id is the image field in Rest Api.
Here is My code:
params.put("title", title);
params.put("comp_logo_id", comp_logo_id);
params.put("company_name", company_name);
params.put("industry_selected", industry_selected);
You must convert image to base64 and upload to server .
In server convert base64 to image .
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
String encodedImageData =getEncoded64ImageStringFromBitmap(your bitmap);
params.put("comp_logo_id", encodedImageData );
Related
I know how to send file using multipart but this time i'm using just a hashmap for sending a file but its not sending here is my code:
WebApi client = ServiceGenerator.createService(WebApi.class);
UserInfoFieldsModel userInfoFieldsModel = new UserInfoFieldsModel();
userInfoFieldsModel.file = view().returnFile();
Map<String, Object> queries = new LinkedHashMap<>();
//
queries.put("first_name", values.get(0));
queries.put("middle_name", "");
queries.put("last_name", values.get(1));
queries.put("mobile_number", values.get(3) );
Uri uri = Uri.parse(ImageUtils.compressImage(
context, userInfoFieldsModel.file.getAbsolutePath(), "fitbook_image"));
File file = new File(uri.getPath());
queries.put("photo", file);
here is my web api:
#PUT(CREATE_USER+"/{id}")
Call<BaseResponse> updateUser(#Path("id") String id,
#Body Map<String, Object> params);
Here are the fields that i'm going to send my data with, the back - end dev said that i should send it as file.
convert your photo or file to base64 and send that file..
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
Log.d("encodedImage", "getStringImage: "+encodedImage);
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Log.d("decodedByte", "getStringImage: "+decodedByte.toString());
//sharedPrefManager.storeImage(encodedImage);
bitmap.getAllocationByteCount();
Log.d("bitmap_size", "getStringImage: "+bitmap.getAllocationByteCount());
return encodedImage;
}
Uri uri = Crop.getOutput(result);
bitmap =MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
image = getStringImage(bitmap);
And say your back end team to convert that image to base64 and insert into database
I want to upload my gallery image to localhost database in the form of string from android but i did not found any relevant topic. Please guide me
you can convert you Image bitmap to base64 String by this code :`
`ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
In Java server I fetch image from external service URL like:
InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));
Then on Android I parse it like:
byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Result: Image not displayed, ain't errors/exceptions neither on server nor on client.
What is the problem here?
EDIT: On android I use class android.util.Base64
Thanks,
Use Picasso library to load image:
You just need to add 1 line of code to show the image on ImageView
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);
You can learn more from here
use this to convert to base 64
public static String uploadPic(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
check if image is uploaded then using volley String request object download the string response using this code convert it back.
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
As commented, let's assume base64Content is the base64 string responsed from your web service/server-side app, you can refer to the following sample code:
String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Moreover, if your server compressed reponse data either by gzip or deflate, your client app must decompress the data first.
Hope this helps!
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
I have an Android application which sends an image to a web service. I want to send the same photo back from the web service to Android.
I made a test program to compare the base64 data that's sent from Android to the server and the base64 that's sent back from server to Android -- they are exactly equal.
I want to use the base 64 string to create a bitmap, so I tried this:
String image = client1.getBaseURI("restaurantFoods/OneFood/"
+ this.getID() + "/getImage");
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
if(decodedByte == null){
Log.d(this.getFoodItem().getName(), image);
Log.d("isNull", "Yes");
}
else{
Log.d("isNull", "No");}
I keep getting null because the log just prints "YES".
Can anyone please help?
If you want to know how I encode the image it is as follows:
private String getBase64(Bitmap bitmap) {
String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
Base64.NO_WRAP);
return imgString;
}
private byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.pizza);
String iconBase64 = this.getBase64(icon);
Try this to bitmap;
public Bitmap convert(String img){
byte[] b = Base64.decode(img, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
And this to String
public String convert(Bitmap bm, int quality){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] byt = baos.toByteArray();
bm.recycle();
return Base64.encodeToString(byt, Base64.DEFAULT);
}
Really I don't see any real problems with your code, but these have worked for me so I suggest that you try them and see if that is actually your problem.