Array of bytes from View in android - android

I have a drawing view, and i am trying to get array of bytes from that view on button click
this is my code:
public void onClick(
drawView.setDrawingCacheEnabled(true);
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), drawView.getDrawingCache(),
UUID.randomUUID().toString() + ".png", "drawing");
}
}
please help!

If view is ImageView first convert into bitmap then convert into byte array
Convert ImageView to Bitmap
public static Bitmap getImageViewAsBitmap(ImageView imageView) {
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
return bmap;
}
Convert Bitmap to Byte Array
public byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
Convert Byte Array to Bitmap
public static Bitmap getByteArrayAsBitmap(byte[] bitmap) {
Bitmap bm = null;
try {
bm = BitmapFactory.decodeStream(new ByteArrayInputStream(bitmap));
} catch (Exception e) {
}
return bm;
}
To call these methods like that:
provide imageview give bitmap
Bitmap m1Bitmap= getImageViewAsBitmap(myimageView);
provide bitmap give byte array
byte[] mbyteArray =getBitmapAsByteArray(mBitmap);
provide byte array give bitmap
Bitmap m2Bitmap= getByteArrayAsBitmap(byte[] bitmap);

Related

Issue in Covert from String Base64 to Bitmap

Code stuff for convert from Bitmap to String Base64
Bitmap thumbnail = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumbnail.recycle();
byte[] b = baos.toByteArray();
String attachment = Base64.encodeToString(b, Base64.DEFAULT);
Code stuff for convert from String Base64 to Bitmap
byte[] encodeByte = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
imageView.setImageBitmap(bitmap);
but i get bitmap = null;
I also refer Base64 to Bitmap to display in ImageView
Thanks in advance.
// convert Bitmap to String
public static String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] arr = baos.toByteArray();
imageData = Base64.encodeToString(arr, Base64.DEFAULT);
return imageData;
}
// Convert String to Bitmap
public static Bitmap StringToBitMap(String image) {
try {
byte[] encodeByte = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
You are calling thumbnail.recycle(); after loading bitmap,, either remove it or call before loading bitmap

Convert String to Bitmap

In my project i will retrieve image from mysql database and display it to Imageview. In database I have saved the link of image. So I need to convert String to Bitmap to display image. But i got error like setImageBitmap is undefined for the type of String. I am not sure what mistake i have done.
Code:
Bitmap b=StringToBitMap(Qrimage);
imgg.setImageBitmap(b);
public Bitmap StringToBitMap(String image){
try{
byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
InputStream inputStream = new ByteArrayInputStream(encodeByte);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
change your function
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte = Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}
catch(Exception e){
e.getMessage();
return null;
}
}
// second solution is you can set the path inside decodeFile function
viewImage.setImageBitmap(BitmapFactory.decodeFile("your iamge path"));
hopefully it will work for you
If you get bitmap = null, you can use:
byte[] decodedString = Base64.decode(pic, Base64.URL_SAFE );
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

android decode string base 64 to bitmap

Hi guys I wanted to ask you one thing, I have a chat that transfers strings and I can even attach of JPEG images before sending them to convert it into a string and then decode in BITMAP just that when I decode it crashes the app. I wanted to know if it is the right code to decode it.
NOME = (TextView) row.findViewById(R.id.comment);
NOME.setText(coment.comment);
String a = NOME.getText().toString();
if(a.length() > 1024 )
{
byte[] image = Base64.decode(a, 0);
int lung = a.length();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, lung);
Image = (ImageView) row.findViewById(R.id.image);
Image.setImageBitmap(bitmap);
}
The code looks fine, if I had to guess I would say you're getting the Out of Memory error, which is very common when loading images. Check out
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
for some best practices when loading images.
The method for Encoding an Image to String Base64 :
public static String encodeToString() {
String imageString = null;
try {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
imageString = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return imageString;
}
The method for Decoding String Base64 to Image :
public static void decodeToImage(String imageString) {
try {
byte[] imageByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
image_view.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
}

Android - How to convert picture from webview.capturePicture() to byte[] and back to bitmap

I'm trying to capture the picture I'm getting from webview.capturePicture() to save it to an sqliteDatabase, to do I need to convert the image to a byte[] to be able to save it as a BLOB in my table, and then by able to retrieve that byte[] and convert it back to a bitmap.
Here is what I'm doing:
Picture p = webView.capturePicture();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
p.writeToStream(bos);
byte[] ba = bos.toByteArray());
I then retrieve the image by:
byte[] image = cursor.getBlob(imageColumnIndex);
Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);
I'm able to retrieve the byte[] just fine but I get a null bitmap all the time from bitmapfactory.
I also notice that if I log.d(TAG, ""+bos) I get a long sequence of bytes as expected but if I do the same to ba just after I do bos.toByteArray() I just get a short array, some thing like this: [B#2b0a7c60
I'm guessing I'm having trouble perhaps to convert by OutputStream to byteArray. Could this by because capturePiture() method returns an OutputStream instead of a ByteArrayOutputStream?
Any help would be appreciated.
Use the below two function convert::::
public String convertBitmapToString(Bitmap src) {
String str =null;
if(src!= null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
byte[] byteArray = os.toByteArray();
str = Base64.encodeToString(byteArray,Base64.DEFAULT);
}
return str;
}
public static Bitmap getBitMapFromString(String src) {
Bitmap bitmap = null;
if(src!= null){
byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
}
return bitmap;
}
Updated::
//Convert Picture to Bitmap
private static Bitmap pictureDrawable2Bitmap(Picture picture){
PictureDrawable pictureDrawable = new PictureDrawable(picture);
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}

Bitmap toString and back again

in android I took a picture by the cam and returnded it to my activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constatnts.ANSWER_TO_LIFE_UNIVERSE_AND_EVERYTHING && data != null && data.getExtras() != null && data.getExtras().get("data") != null) {
Bitmap snapshot = (Bitmap) data.getExtras().get("data");
String convert = InputOutput.bitmapToString(this, snapshot);
Bitmap back = InputOutput.stringToBitmap(convert);
}
}
When I assign the Bitmap 'snapshot' to an imageview it loosk pretty good an works well. But when I assign the Bitmap 'back" to an imageview it does not change its view. So there must be something wrong in transformation. Here is my code for the tranformation:
public static Bitmap stringToBitmap(String bitmapString) {
byte[] bytes = Base64.decode(bitmapString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
public static String bitmapToString(Context context, Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmap.recycle();
byte[] byteArray = stream.toByteArray();
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
stream.write(byteArray, 0, byteArray.length);
stream = null;
String strBase64 = Base64.encodeToString(byteArray, Base64.URL_SAFE);
return strBase64;
}
Any suggestions what goes wrong here? Thanks!
Here's a code I used once to try this conversion, it should work:
public final static String bitmapToString(Bitmap in){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
in.compress(Bitmap.CompressFormat.PNG, 100, bytes);
return Base64.encodeToString(bytes.toByteArray(),Base64.DEFAULT);
}
public final static Bitmap stringToBitmap(String in){
byte[] bytes = Base64.decode(in, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
You might want to add some close() calls to the streams though.

Categories

Resources