Save Image files and some text together as one file/object - android

I created an android test app with camera interface in which I want to save a text message and image file as one object. I am able to enter message, call the camera,take a picture and also populate the image in the image view on the app. Now I want to save them as one single record/object so that I can transfer it over network using protocol such as ftp. How to save image file and text together as one single file/object? Could someone please tell me how to do that. Thanks in advance.

There are lots of possibilities, and it depends to some extent on what will be consuming the file after it is transferred. You could, for instance, simply serialize the string and the image data using a DataOutputStream wrapped around a FileOutputStream. As a fancy version of that, you could define a class to contain the text and the image, have that class implement Serializable, and serialize it to a file. Alternatively, you could serialize the image data as a base-64 string and then put the text and the base-64 image data into an XML document or JSON string. Other approaches are also possible.
If you provide more details about what kind of process will consume this file after it has been transferred, perhaps we can provide more focused suggestions.

Related

ANDROID - display image and text from separate web service calls

I need your advice or ideas regarding my problem. The app I'm working on needs to display an image and text from two separate web services. The first web service (image) returns a base64 encoded string and the last web service(text) returns a plain json response.
Some samples I found on the web is that they only use one service to display the record with the the string url of image something like this:
[{"id":"1","version_name":"Alpha","description":"","version_code":"1.0","api_level":"1","image":"http:\/\/www.test.com/img/cupcake.jpg"}]
But in my case, the client has given me two separate calls to view the data (details) and image:
[{"id":"1","version_name":"Alpha","description":"","version_code":"1.0","api_level":"1"}]
for image Base64 String result (SAMPLE):
"iVBORw0KGgoAAAANSUhEUgAAAGQAAABfCAYAAAAeX2I6AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAACxIAAAsSAdLdfvwAAEc8SURBVHhezb13dBzHmT3qc37nvV1bXq+9tvfn9Xpt79q78tpWdJAsKllWJsWcwRyRc2QQgyiJogIpkiIJEDkMcmLOOSeJOZPIwOQ8gxkAvO9+NWhwBIE0Kck+7497uru6urr7u/Wl6urub+g6SpDfVYIcVHBZhbxblfeMXHCJUuR0ViC7s0ohE9VIJ1az/DbKWFahkMFj1nI7u6sCeV21yL1VhbLOYuhQjOqOXNShAFbWcHV9AheXdmQpOJANJ5ceLt08i8BxK5/7ctU+B/e5b+XB2ZlD5MLZlUVksp1suG6xjOsCtzo+B/ZbRTymGLauPK6znVuFatsh52c9gQNFsHUSXflcl3Pksb1cIl+16brF9rpKed5CgudUbRSptgLtFdwznDze0VmAr0RINoWbyZvIoNBzb1HAJCansxzZt8qR21V9G6ybc6usBxm
To display this in a listview, how would I display them at the same time? How would you create a workaround for this?
I would truly appreciate your help. Thanks.

Android Save EditText with Image

Right now I have a simple App that takes a picture and saves it to a folder. I'm trying to figure out a way to have a user answer questions about each picture (I would just be using EditTexts with that). Is there a way to connect those EditTexts with the image so that when I would open up the image on another page that i could click a button to view the information that was entered along with that image? I have an idea on how to code everything else, just not keeping the edittexts associated with the image. Any ideas would be greatly appreciated!
Thank you!
perhaps you could create a data file alongside the image that has the same name as the image file, but a different extension.
So if you are saving an image called img1.png you save another file alongside it called img1.txt which contains the data you collect from the EditText(s). How you format this data file will depend a bit on how much data you have to store, and what kind of structure the data needs to represent (if any)
Then when you load up a png to display it you'll need to also load up the txt file that shares the same name, and populate the data contained inside to some TextView or something near your ImageView.
Use a database. Make a table that associates images with the data you want. When you display the picture, read the info for that picture from the database and display that as well. Either store the images directly in the db or store them in a folder and use the filename as the primary key in the database table.
There's number of ways to accomplish this. You could use a hashmap where the String is the data they entered and int is the resourceID of the image. For persistent storage, I recommend an SQLite Database that stores the string in a column and the associated image in another.
A few options:
Save a file with the image.. Give it the same name but .txt or something. The file should probably be stored in the location returned by getFilesDir() . Alternatively you could use SharedPreferences.
Use an sqlite database
Save within the exif data of the image. Use the ExifInterface class with setAttribute/getAttribute to save the values.

Array list of double in SharedPreference

I want to know whether it is possible to save as an array list of double in SharedPreference. In my application, I want the 'size' to be saved whenever there is new 'size' written from the user. It must be uploaded to an array list of double, without erasing the previous one.
first of all - the answer to your question is simple:
it's not possible.
but the good news are that there are lots of ways saving array of doubles (as you've been suggested in the comments):
save it to a binary file / serialized file in the internal/external storage, to SQLite database, make it the responsibility of the server side (if there is one..)
you can save the size of the array in the shared preferences if it helps you read the data from the file containing the serialized array, but I guess you already know that..
anyway - good luck

Decode JSON base64 data (from PHP's base64_encode) in Android?

This is my JSON code. Note the title field is base64_encoded. I want to get this title field value.
{"item":{"id":"1","title":"ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==","status":"1"}},
{"item":{"id":"4","title":"ZGVtbyBldmVudCBmb3IgZGVtbyBkZXNjcmlwdGlvbg==","status":"1"}}
{"item":{"id":"6","title":"ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==","status":"1"}}
The title text is in base64 format (ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==) but I would like to get it as normal text -- how can I do this easily?
Thanks.
unfortunately the entire point of encryption is to stop people from doing this...
If you are legally obtaining the encrypted data, try getting in touch with the server team or checking their documentation to find out how they encrypt and how you should decrypt.
There isn't a one size fits all solution for this problem. The answer is 100% dependent on how the String is encrypted in the first place.
good luck!

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