Re-using byte array data from server - android

I'm having trouble understanding how I can re-use byte data sent down from my server. I'm trying to set up a stub backend for an app and be able to store/return all the backend data locally for a specific user.
When setting a breakpoint and copying the value of my server response (byte[] type), it looks like
[-119, 80, 78, ...]
when copied into a text editor.
If I wanted to store this large array locally and return the value as a byte[] back to my server response handling, how can I accomplish this?
I tried storing the value in a String variable and using getBytes(), but the compiler complained that the string was too large. And I'm not even sure that would have returned what I wanted anyways.

store it as a BLOB in SQLite. In this example --> http://sunil-android.blogspot.com/2013/10/insert-and-retrieve-image-into-db.html BLOB is being used to store an image that has been converted to a byte array. 1mb limit..

Related

Is it good to encode each string in BASE64 for storing in Mysql?

In my android app, each string stored in mysql database is encoded.
I do this because i have emoji in strings, and this is the only way i found.
Encode :
byte[] data = str.getBytes("UTF-8");
String base64String = Base64.encodeToString(data, Base64.DEFAULT);
Decode :
byte[] data = Base64.decode(userObject.getString("mystr"), Base64.DEFAULT);
String question = new String(data, "UTF-8");
You're right, you should save the text data in a simpler way. The problem you mention with emojis can be resolved changing the default character set of your database tables to utf8mb4.
You can accomplish that by doing the next steps:
Change the character set of your tables in the MySQL database using this SQL statement
like this:
ALTER TABLE your_table_name charset=utf8mb4,
MODIFY COLUMN your_fieldname1 VARCHAR(255) CHARACTER SET utf8mb4,
MODIFY COLUMN your_fieldname2 VARCHAR(255) CHARACTER SET utf8mb4;
Add this to the MySQL conf file and restart or reload the server (usually located at /etc/mysql/my.cnf or similar)
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Everytime you access your database in your Android application
execute the next query: SET NAMES utf8mb4;
Query your text column as a normal String object in Java and you should see the emojis correctly displayed (no need for byte array or Base64 encoding).
According to wikipedia:
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport.
Unless you have a specific reason for doing so, I don't recommend passing everything from your app to the backend web service in Base64.
Maybe, you can use a BLOB type field on your sqlite database...
BLOB. The value is a blob of data, stored exactly as it was input.
Sqlite.Org - DataTypes

Android Wear: How to send multiple items with MessageApi?

So i want to use the MessageApi and not the DataApi to send multiple items to the wearable. However the sendMessage() of the MessageApi only allows a byte array to be sent.
For reference:
Wearable.MessageApi.sendMessage(mGoogleApiClient,
String id,
String path,
byte[] bytes)
My guess would be to create an array of byte arrays and then serialize that into one big byte array and send it off.
To make things clearer these are the steps in pseudo code:
byte[][] arrayOfByteArrays;
String a --> convert to byte[];
Bitmap b --> convert to byte[];
Add a and b byte[]'s to arrayOfByteArrays;
Serialize arrayOfByteArrays to just a byte[] and send it off;
Is this the correct approach? or Should I just call sendMessage() multiple times? Thanks.
In general, it is more efficient (battery, bandwidth, ...) to send one message instead of multiple ones. However, there may be other factors involved in your specific case that may warrant sending multiple messages. Looking at your pseudo code, I noticed you are also trying to send a bitmap using the MessageApi. That, in general, is not the best approach to send across an image, or other types of assets; you can use Assets or you can use ChannelApi. Otherwise, the approach of serializing a bunch of small objects and putting them into one byte array and then desrializing at the other end would work; one simple way to do so (if dealing with simple objects) is to use json as a serialization means.

Android: System.out: "resolveUri failed on bad bitmap Uri:[B#4072aa78" Prompt when resotre image stream to ListView

I have referred couple of similiar articles regarding the message prompted from System.
The image string is a byte array type Bitmap format. It was encoded to string format by using andorid's Base64 class tool. Then it was saved to mysql DB with Blob format.
On my new App, I want reload the Blob image sting to ListView and show the image: I tried two approaches, but all were failed loading image to listview:
a. put the re-loaded Blob imange(it is a string text type) as value of HashMap's key/value pairs. And then,initial an Adaper (ex. SimpleAdapter) to load the key/vales' and then try to show it on ListView(failed).
b. Similar a, but use Base64 decode method to decode the Blob image back to byte array first. And take it as Byte array type as value of hashMap's key/vale pair.(failed)
I have had studied this problem couple of days and no progress for this problem. If I used wrong process before, Please guide me to correct it, thanks !
By the way. the stored Blob image string can be re-loaded and display to ImageView using
imageview.setImageBitmap(bitmap) method without problem. So, the stored image string data is valid bitmap data. However, this method can not be used on ListView.

getting image from byte array in JSON object to android app

Heres my situation:
I have a RESTful WCF service running on my server, the service is meant to get various types of data about people from a database and makes that data available as a single JSON object. It works great!
[EDIT]There is another service that maintains an image cache in the file system on the server. When a request is sent to the RESTful service, that service then requests an image from the image service. If the image is already in the cache (same width, height and person as the request), it returns that (as a byte array). We MUST use this service to retrieve images.
Now what I want is the image of that person. In our database, the image is a long raw (ew). However, I have dealt with that issue already (previous paragraph). The image is now a Byte array. I am pretty new to android and I am not sure what the best way to retrieve this image is. What I thought I could do was add the byte array to the JSON object and then use the base64 decoder to convert it into a drawable image. However, everytime I try, it times out and tells me it expected ',' or ']' at some arbitrary index of the char buffer for the JSON object.
I have been able to pull the small bits of data out of the JSON object without an issue, but now that there is a huge byte array in it, the JSONObject hates me. What would be a better way to get this image from my service?
Base64 encode the byte array to get a string.
Add the string to JSON object and send it.
When JSON is received, get out the string.
Base64 decode it to get back the byte array.
Use byte array to create Image.
See this question on storing images, it's always better to store this sort of data on file system. If possible deprecate that field, and create a script to move existing images to file system.
You should then store the images on a file system (or some sort of content management system) which can be retrieved by a URL.
Then store the URL in the database. you can then send this in your json object.
{
...
image_url:<url from database>
...
}
When the client receives this it will make a call to that URL and download the image.
Your client will have to make a separate call to retrieve the image but it's generally better than filling your database with binary data. This can also work to your advantage if you want to display data fast while allowing the image to be downloaded in the background.
Better than using Base64 encoding is this way of returning Stream (from WCF RAW programming)
[OperationContract, WebGet]
public Stream GetTestImage(Image image)
{
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return stream;
}

Include Small Image in JSON for Android App

My app currently requests a JSON file with some text and other data from my server. I want to add functionality so that it also downloads a very small image (like an icon) through the same file [without creating an additional request]. Is it possible to do so, and how would I go about it (base64?)
Should be eminently reasonable: look at http://developer.android.com/reference/android/util/Base64.html. All you'd need to do is:
Read your icon into a byte[] array on the server.
(Assuming your server is in java) Use something like http://iharder.sourceforge.net/current/java/base64/ to write the byte[] array into a StringOutputStream through http://iharder.sourceforge.net/current/java/base64/api/index.html?Base64.OutputStream.html.
Add the contents of the String to the JSON file.
On the android device call http://developer.android.com/reference/android/util/Base64.html#decode%28java.lang.String,%20int%29 to convert the JSON attribute into a byte[] array.
You can then pass the byte array to http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29 or one of its brethren functions (you may have to play with image formats/encodings to get it to swallow your byte array correctly).
Voila! You have a Bitmap you can use.
Let me know how that works out.

Categories

Resources