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
Related
In general vector drawables are compiled, and accessed with their ids in execution time.
My project consist of pulling out images from a server. For now, i'm doing it for png files, but i would try it with vectors ".xml".
I'm able to download those, but i can't parse them in drawable type to show them in views.
How can i do this with a bunch of bytes representing the vectors resources and parsing them to drawables ?
It would be a huge thing for my app since it will be much much lighter, and good for those pixelized pictures.
The inflate method may be helpful, but you could use a third party library:androidsvg. Don't reproduce the wheel if possible.
If you can access the image from the server via url, use Glide to load it to your View. By so doing, you don't have to download the image to your storage before using it in an ImageView. However, if you still want to get the image as a byte array and display it in a view, use the code snippet below...
Bitmap bm = BitmapFactory.decodeFile("/path/to/yourimage.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray(); //This is the image byte
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); //Encoded string
Furthermore, you can convert the string to a Bitmap and then pass it to your ImageView.
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap bmp= BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(bmp);
Let me know if you have any question concerning this, I'd be glad to help.
Is there any way to store image files in firebase using Java api to be used for android application?
I have read this thread Can I store image files in firebase using Java API and still there is no answer. I know that there is an official api for it, called firepano here is the link https://github.com/firebase/firepano but it is for Javascript library.
is there any solution for Java library??
I used this code and now it's working:
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.chicken);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
String imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
In my application,I want to download images from an URL and then save them in a safe area in Android device. But I have no idea about storage options in Android. I don't want to use sdcard or another external storages.
Though it's not a good practice to save your application data in the limited internal storage of the device you can do that.
Check the official google documentation for saving data in the internal device storage which will give you lead about storing stuff in the internal.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
use iternal storage http://developer.android.com/guide/topics/data/data-storage.html#filesInternal and force app to instal in internal http://developer.android.com/guide/topics/manifest/manifest-element.html#install
on non rooted devices should be enough ...
I suppose this could be useful for you (as it was for me): https://github.com/thest1/LazyList
You can then convert you image > Bitmap > ToBytes and then into Base64 String object. By this you will get a String representation of your Image and you can simply store that into SQLlite DB.
Warnig : This may lose image quality.
TO convert drawable into Base64 String.
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
imgString = Base64.encodeBytes(b);
To convert String back to drawable. Get String from SQLite DB and then
byte[] b = Base64.decode(imgString);
Bitmap bc = BitmapFactory.decodeByteArray(b, 0, b.length);
Drawlable drawabe = new BitmapDrawable(bc).getCurrent();
I want to upload an image to my website via my android app so for that i want to convert my image to Byte[].
i have used the following code but not work for me..
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG,0, bos);
byte[] data=bos.toByteArray();
So please share with me any other way to convert an Image to Byte[]..
Use ByteBuffer:
array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);
and use array the way you want...
Simply put I need to capture image using camera and upload it to facebook via my android application. And I successfully did that. The problem is when the photo posted in facebook, it's just too small and in low resolution while the image I took is in high resolution.
I understand that: in order to upload to facebook, i need to convert the captured image which is in bitmap format into byte array. So i have method for that:
public static byte[] convertBitmapToByteArray(Bitmap bm){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
Then to upload the image to facebook, i have code below where byteData is byte array I converted from bitmap image using the method above.
parameters.putString("message", "Test");
parameters.putByteArray("source", byteData);
String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST");
return facebookResponse;
I am pretty sure the problem is my convertBitmapToByteArray method since the method is to compress the bitmap image and turn it into byte array, and this made my image into low resolution image. However I can't seem to find the way to upload the image without converting it into byte array first. Any solution for me?
Alright even this thread is old, i found out the answer. It's not the problem of CompressFormant.JPEG or CompressFormat.JPG. Simply put, intent in android isn't designed to carry big data like image from activity through activity. I need to save the image from intent to sd card first before able to pull it out from there. It's my solution.