Convert String to Bitmap - android

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);

Related

Unable to convert bitmap to string then back to bitmap using BitmapFactory.decodeByteArray

I'm base64 encoding a bitmap to a string in order to send it as part of a JSON object. The recipient of the image will get the string and convert it to an image again.
If this is at all relevant, I'm doing this for an Android app.
Converting the bitmap to a string seems to work ok, but when converting the string back to a bitmap I'm getting a NullPointerException.
I've tried to boil this down to the basics (and converting to a string and back to a bitmap in the same method for testing), so I have the following:
public static void convertBitmapToBase64String(Context context, String filename, int maxStringSize)
{
Bitmap originalBmp = PicUtils.getBitmapFromFilename(filename, null, -1);
String base64Image = PicUtils.convertBitmapToBase64StringFromFile(context, TEMP_FILENAME);
// The encoded string is not null, so encoding seems to work.
DataUtils.log("base64Image length is " + base64Image.length());
// Test if we can convert back
final byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
// This returns null! Is failing here.
Bitmap decodedByteBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}
public static Bitmap getBitmapFromFilename(String filename)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, options);
Bitmap finalBitmap = BitmapFactory.decodeFile(filename);
return finalBitmap;
}
public static String convertBitmapToBase64StringFromFile(Context context, String filename)
{
try {
FileInputStream fis = context.openFileInput(filename);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Base64OutputStream outputStream = new Base64OutputStream(byteArrayOutputStream, Base64.DEFAULT);
IOUtils.copy(fis, outputStream);
return new String(byteArrayOutputStream.toByteArray(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
DataUtils.log(e.getMessage());
return null;
}
}
Any tips are appreciated!
This is a link I used to solve my problem. I hope this will work for you too.
http://androidtrainningcenter.blogspot.in/2012/03/how-to-convert-string-to-bitmap-and.html.

Error in Base64 string To image

android code Error :
byte[] decodedString = Base64.decode(""aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTI2NQ=="", Base64.DEFAULT);
Bitmap base64Bitmap = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
Log.d("img", String.valueOf(base64Bitmap));
imagview.setImageBitmap(base64Bitmap);
logcat Message
SkImageDecoder::Factory returned null
Your base64 string is corrupted.
Check it via below link:
http://codebeautify.org/base64-to-image-converter
Please try to decode some different string and then check it.
or try below code :
byte[] encodeByte = Base64.decode("aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTI2NQ", Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
If still it won't work try Base64.NOWRAP instead of Base64.DEFAULT.
Try this
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
ImageLoader imageLoader = ImageLoader.getInstance();
ImageView imageView = (ImageView) this.findViewById(R.id.imageView);
try {
url = decodeBase64String(base64String);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
imageLoader.displayImage(url, imageView);
String decodeBase64String(String encodedString) throws UnsupportedEncodingException {
byte[] data = Base64.decode(encodedString, Base64.DEFAULT);
return new String(data, "UTF-8");
}
set dependency -compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
Check this 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;
}
}

Array of bytes from View in 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);

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;
}

Categories

Resources