Image from SD card to byte[] - android

how can i get the byte[] of an image from android emulator SD card?
here's my current code:
File f = new File("/mnt/sdcard/test.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.iv);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
What i want is the byte format of the image how can i do this?

Create a ByteArrayOutputStream then call compress() method of Bitmap so the contents of bitmap will be passed in ByteArrayOutputStream and converted to byte[]
public byte[] bitmapToByteArray(Bitmap b)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
To apply,
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
byte[] bitmapBytes = bitmapToByteArray(bmp);

You can convert Bitmap to byte[] like :
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Folder/" + filename + ".PNG";
Bitmap bmp = BitmapFactory.decodeFile(imageInSD);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Related

How to convert image to string in Android?

It's my MySql DB
Its encoding:-
public String convertBitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeToString(byte_arr, 1);
return imageStr;
}
This is decoding:-
String img=o.toString();
byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Thank you.
It could be done by
private String getBase64String() {
// give your image file url in mCurrentPhotoPath
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// In case you want to compress your image, here it's at 40%
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
And for Decode
private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) {
// Incase you're storing into aws or other places where we have extension stored in the starting.
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
Bitmap bitmap = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(bitmap);
}

Unable to convert image bitmap to byteArray

I am using the following code to encode my image bitmap into a byte[] and then to a string but at the time of encoding it into byte[] it says unable to encode..
Bitmap bm = BitmapFactory.decodeFile(path);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
(bm).compress(Bitmap.CompressFormat.JPEG, 100, stream);
bm.recycle();
byte[] byteFormat = stream.toByteArray();
String encodedImage = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
The app is working fine, but I am stuck here. Suggest issues.
public static byte[] bitmapToByteArray(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bitmap.recycle();
return byteArray;
}
Try this for me this works :)
public String imageToBaseString(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bm = BitmapFactory.decodeFile(path, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}

Convert Image to Base64 String Android

I am able to get an encoded String using the following code :
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.NO_WRAP);
}
BitmapDrawable drawable = (BitmapDrawable) imgPreview.getDrawable();
Bitmap bitmap = drawable.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tzb6vyXk0tk9cH9iPRGku/HV+zSrHa6b4VtcgHYcR3V26GQgKhInSTgHjgAkFh/SV8KI28QfsjeJNPZxJD/AMIv4kgixuy2JNbhGzBHyR+Wq/3gUHVChP4HfskaUmk/DTxXf7PNk1rxpf20MkaANNY6Hp+n6eSSQuEWaOdIiuSHEwLBo2U/vT+y4s0/7OXiPR3djPZWevRPCzZk/wBIluLxsg52iRrqV1VsMEZSy5LE/c4Spz5lj7O96MoJ9PdUl16O731Wl7NnymIpcuXYJvRxq0pPX+abu7W62X33vdM/Mi9uQ+nG6Mwjt7a0F3OUwI4rdFbIchiSURDIUySSWO4cmuH+GdlNqGn694xvIAtz4p1RHsRI8pkj02Jmi0qI+ZyqiESXTBTtKEHaFDVzfjDWZ4fCth4UsWf+1PGOpLo8YTBkg0wXEzatcgDJURxDaC4JVXHQ5avc/DNhEYrCyto1FnpqCCNAP+ecbwmQ8DCxwggdBiYBSVJNfJYlezjUSbi3Jx6/z3e99euvTq3qfT4f3oxSStCMU2r76J9XZ6X6fFLZtsvhAbWNJDlWmjlQcZkSGRXgRwRtCmVYpZPl5iWYL8zAnq7hfsmiW1szN590itclhg72JdiSVUnIUKwPVt4zuOBmqsVxeyEyOINOTftITyyqZAwSOWZ1ZTg52IRjg5bKLnUZYUjcl7mWC2tvkLNH5zOXb5jgmOIM+eijeyqSGY+RGPNfyt+fr21/Dc75JaJ3trb5Nd393m1ro7uuLs6J4Xur6ND5mqTfZoFjAaaS0DbGjUOw2veTbIAUIKpPuUAxsTL4at40jmFxGsgtVa+vZB8wvNSld5JdwJG95LotHBGCwSNURCylmrnfFOtQXfiu30W2Rp
But when i try to decode it using http://freeonlinetools24.com/base64-image nothing shows.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
byte[] byteArray = byteStream.toByteArray();
String baseString = Base64.encodeToString(byteArray,Base64.DEFAULT);
This worked for me.
I have found out the problem. The problem was with the logcat from where i was copying the encoded string. Logcat didn't display the entire String so the broken string didn't decode the image for me.
So when i passed the encoded string directly to the decode function the image was visible.
I checked an image with given link http://freeonlinetools24.com/base64-image and it works fine. I think the reason for getting wrong output is your string. Encoded string always ends with == . Check your encoded string.
Example Code
Encode Image
BitmapDrawable drawable = (BitmapDrawable) imgPreview.getDrawable();
Bitmap bitmap = drawable.getBitmap();
encodedImage = encodeToBase64(bitmap, Bitmap.CompressFormat.JPEG, 100);
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
Decode Image
Bitmap bitmap = decodeBase64(encodedImage );
img.setImageBitmap(bitmap);
public static Bitmap decodeBase64(String input)
{
byte[] decodedBytes = Base64.decode(input,0);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
EDITED
see my string..
iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAbhUlEQVR42u2df5BV5XnHn7u7yu4CsoATcRRZbWSxMxHWfxRTcdUwNJqJpBpNqlO22lE7YkGTaFKMXmaw0bRVbDFgKrj8kTGVZgSjSR1RVwXRZopLmk4EE10KjZqKLCi/NGrf595zlrNnz7nn53veH+f7mTmc/XH33vcu+3zf9/nxPm+FgLEsXfLFDnGb5Xza49w7nYvh78/M+DLbxDXkfDzoXEy/cx+4866fDyV7SqALFdUDANEIQ2cj76S6sfPFhn2+6nH5eJ7qQjHgXINCGAZUDwo0BgKgGY6x99BRY886g6uGVxCuKPRDFPQCAqAYYfA9VDd4vnSb1WXBq4V+qgtCv+rBlBkIQMF4Zvj5VB6Dj4IFYT1hhVA4EIACEEbPxt5DdaOfpno8mrOTjorBetWDsR0IgCQco3evCarHYyj7qC4G6yEGcoAA5IizvF9MMHoZuGKwHG5C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f2dzctBLL/GxAAHLEqS5kIeA9B9rGCkzsNuT49k+J2f4fylq1JwMIgCScjUfupaUY6F5C7DH6x8q0QadIIAAF4IhBD9XFQCs3QbduQ7y8F9e/C6N/FkYvHwhAwThuQg/VxUCLjUiquw05G3J+Kj7ciOV9sUAAFONkE9xLqSAU1W2IDV786W0Ss/wziN6rBQKgGZ4VwiznKrTNed7dhriNtrh+JYx9gDDDawcEwAAcUeiko6LAqUdpq4U03YbqszrtF39SvxTGvlV8/FsYu/5AAAzGqUGY5Xza49w7nYvh76daQbjdhj788GMxg9O++lcru8Q/O2sfieU735ubm15BLt5c/h/s/pXAGnPaugAAAABJRU5ErkJggg==
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();
Encode/Convert image to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

convert image.jpg to Base64 string

error log is image:null
my problem is not proper convert image to base64.
my code encode image
BitmapDrawable drawable = (BitmapDrawable) ivProfilePic.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bos); byte[] bb = bos.toByteArray();
String encodedString = Base64.encodeToString(bb,Base64.DEFAULT);
Log.d("ImageBase34", encodedString);
decode string to covert image
byte[] decodedString = Base64.decode(userDetailBean.getProfile_picture().getBytes(), Base64.DEFAULT);
Bitmap base64Bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Log.d("img", String.valueOf(base64Bitmap));
ivProfilePic.setImageBitmap(base64Bitmap);
Use the encodedString as the first parameter in the Base64.decode() method instead of userDetailBean.getProfile_picture().
You need to change your code like this.
BitmapDrawable drawable = (BitmapDrawable) getDrawable(R.drawable.ic_default_user);
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bos); byte[] bb = bos.toByteArray();
encodedString = Base64.encodeToString(bb,Base64.DEFAULT);
Log.d("ImageBase34", encodedString);
decode string to covert image
byte[] decodedString = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap base64Bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Log.d("img", String.valueOf(base64Bitmap));
imageView.setImageBitmap(base64Bitmap);

Drawable to byte[]

I have an image from the web in an ImageView. It is very small (a favicon) and I'd like to store it in my SQLite database.
I can get a Drawable from mImageView.getDrawable() but then I don't know what to do next. I don't fully understand the Drawable class in Android.
I know I can get a byte array from a Bitmap like:
Bitmap defaultIcon = BitmapFactory.decodeStream(in);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
But how do I get a byte array from a Drawable?
Drawable d; // the drawable (Captain Obvious, to the rescue!!!)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Thanks all and this solved my problem.
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.my_pic);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tester);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
If Drawable is an BitmapDrawable you can try this one.
long getSizeInBytes(Drawable drawable) {
if (drawable == null)
return 0;
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
return bitmap.getRowBytes() * bitmap.getHeight();
}
Bitmap.getRowBytes() returns the number of bytes between rows in the bitmap's pixels.
For more refer this project: LazyList
Here is my Utility function which can be copy-pasted
/**
* #param ctx the context
* #param res the resource id
* #return the byte[] data of the fiven drawable identified with the resId
*/
public static byte[] getDrawableFromRes(Context ctx, #DrawableRes int res) {
Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), res);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
File myFile = new File(selectedImagePath);
byte [] mybytearray = new byte [filelenghth];
BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile));
bis1.read(mybytearray,0,mybytearray.length);
now the image is stored in the bytearray..

Categories

Resources