How to upload an image from Odoo 11 to an Android application? - android

I use XML-RPC to upload images from Odoo-11 to to an android application.
How can I convert the result of a request to a bytes array bytes[]?
Because in the table the format of the field image is bytes[].

You will need to code and decode the image file data as base64 to send and receive the data properly

In Odoo The image files are storing as string (base64) , i think you need to read the image data from odoo to mobile application for displaying in mobile application right ?
you can use the XML-RPC library Odoo External Api
The External API is using For integrate the odoo with other Systems
Check The url , there we have "Search_read" method to read data from odoo
you will get base64 string and then you need to convert the base64 string in your mobile application to display the image
eg : models.execute_kw('res.partner','search_read',[['id','=',1]],{'fields':['name','image']})
you will get the name and profile photo of partner id 1

I there any possibility to use one of the image loading library (glide,picasso.etc) in an odoo mobile framework?

You can just create below function into your activity/fragment.
public static String encodeBitmapToString(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}

Related

how to retrieve the saved photos on the server as byte[] using retrofit2 on android and then display them with Picasso

I've developed a website under MVC ASP.NET using c#. It is to let the users save their data including photos which are then saved as byte[] on the respective database that is SQL server. I've thus designed a web api to retrieve the data as JSON from the database. Plus I've developed a mobile application using which the web API retrieves and displays the data. I used retrofit2 to use web API in android. The mobile application displays all the data correctly except for the photos. My question is: how can I retrieve the saved photos on the server as byte[] using retrofit2 on android and then display them with Picasso?
Define your model:
public class SomeApiResponse {
private byte[] image;
// setter and getter...
}
Now define your service API:
public interface RestAPI {
public Call<SomeApiResponse> callService();
// ...
}
Convert byte[] to Bitmap:
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
And finally load bitmap into ImageView:
imageView.setImageBitmap(bitmap);
And yes, you do not need Picasso to do this!

Posting jpeg file as text in Android

I have a photo (jpeg) format in an Android device. Instead of posting the file as a file using HTTP I prefer to convert that to a string and post it as string using http to a spreadsheet. I understand jpeg files are encoded and opening them as string shows funny characters. My question is if I send these characters as string using http, can I get them back on the other side using a binary file editor and save them as jpeg?
One thing you can do is, on client side:
1.Convert image to byte array.
Path path = yourImageFile.toPath();
byte[] byteArray = Files.readAllBytes(path);
2.Encode the byteArray to Base64 String.
String encodedString = Base64.encodeBase64URLSafeString(byteArray);
3.That's it, send via http.
I'm not really sure what you mean by a binary file editor, but from server side you can retrieve the image like this (if using Java):
byte[] decodedByte = decodedBase64.decodeBase64(encodedString);
FileOutputStream out = new FileOutputStream("newPathToYourImage");
out.write(decodedByte);
out.close();

Cons and pros of using Base64 encode and decode image in Android

This is block of code I used for convert from Image into Base64:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String imgStr =Base64.encodeToString(ba,Base64.DEFAULT);
But I know we also can convert image into Binary data.
So just want to know the cons and pros of using Base64 encode image in Android. Should I use Base64 or Binary data to transfer image through message.
You should not send images through gcm server. It was not designed for that at all. You should be sending links, and other information, which is then used to get the data from a server after being received on the client (server-> client pushes).
Base64 is often used when transferring binary data as a string, which is stupid in this case because it increases the size of the data needing transfered, and you could easily do a POST to a server with the binary data directly.

How to store image in Database?

I need to store in Database images chosen from gallery. My firt idea was convert Bitmap to String and store String in Database, but now I am reading other post: saving image clicked from camera or loaded from gallery in database and retrieving it and there is suggested using byte array.
Could someone explain me diffrence, which idea is better? Maybe something else?
I just start, but I would like to write it possibly correctly.
The standard way to store an image as a byte[] in a BLOB field. Another possibility - with some overhead - is to store a Base64-encoded string.
You can use the Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 aka version 2.2).
Check this article out for a work-around:
http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

Android: get image from base64binary format

I use web service to get image. The service response contains image in base64Binary format. I try to decode response data with Base64.decode() (http://iharder.sourceforge.net/current/java/base64/). See my code below:
byte[] data = Base64.decode(responseString);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bmp);
decodeByteArray always return null.
I try to save data in .png file. I can open this file on my PC and in the Android File Manager application. But preview activity of File Manager couldn't open this file.
Then i try to parse this data using .NET client with Convert.Base64() method. And this image have been processing successfully. Then i compare byte array in image created with android client and .NET client. The differences were in sign of bytes. .NET uses unsigned bytes but Java use only signed bytes. Is this is a reason of my problem?
Is anybody have the same problem in decoding of base64Binary?
Here is one solution, and for me is working (knowing that the format in which the image comes from the server through the web service is base64binary)
decodedIcon[] = null;
byte[] bb = (resposeString).getBytes("utf-8");
decodedIcon = Base64.decodeBase64(bb);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedIcon, 0,
decodedIcon.length);
//then you get the image view and setImageBitmap(bitmap)
PS:
Base64.decodeBase64 comes from the library org.apache.commons.codec.binary.Base64;
You should have commons-codec-1.3.jar included in the assets folder
the version doesn't have to be 1.3
Thanks to one of my friends for this hint.

Categories

Resources