Send a file without using multipart - android

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

Related

How to get video thumbnail from server videos?

Guys anyone please help me how to get video thumbnail from server side video?..
I Searched alot in stack but didn't get any useful solution?..
I used FFmpegMediaMetadataRetriever this library.. I faced few errors..
video thumbnail to server
final File myFile = new File(selectedPath);
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(selectedPath, MediaStore.Video.Thumbnails.MINI_KIND);
String thumbnail = getStringImage(bitmap);
//Server side
params.put("thumnails",thumbnail);
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}

Retrieve image from firebase in android app

How can i retrieve images from firebase . i am converting my images to base64 string first then saving it to firebase string code below.
Bitmap bm = BitmapFactory.decodeFile(imgDecodableString);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] byteArray = baos.toByteArray();
String encodedImage = Base64.encodeBytes(byteArray,Base64.ENCODE);
ref.push().setValue(encodedImage);
Now how can i show this image in my activity.
byte[] decodeImage = Base64.decode(encodedImage,Base64.ENCODE);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodeImage);
imageView.setImageBitmap(bitmap);

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

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

android bitmap isn't created from base64

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.

Categories

Resources