loopj - Android Asynchronous Http Client upload image from drawable - android

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

Related

Send a file without using multipart

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

how to upload an image to server in android

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

Image Doesn't Show in my Android ImageView

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?

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

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

Categories

Resources