Decode base64 string to bitmap failed - java

I am using this to function for encoding and decoding a bitmap for storing a photo in MySQL database. I insert the photo as a string and i want to retrive it in my project to display in an CircleImageView.
public String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
public Bitmap StringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
} catch (Exception e) {
e.getMessage();
return null;
}
}
and this is where i call it:
String fotoFromDB = studentFromDB.getFotoStringStudent();
Bitmap bitmap = StringToBitMap(fotoFromDB); //in the debbuger here says that bitmap==null
profilePic.setImageBitmap(bitmap);
String fotoFromDB = iVBORw0KGgoAAAANSUhEUgAAAjEAAAOwCAIAAABAlrJqAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4 nOy9WZckx5WYee81c/dYMrMysxaCAEHsG0mAa7NJEFKv6m5pelrS0YPOzB8Y9Tyq9RNmXuZBZ/6A ztGcOXNmXlpnRupNvVHdbIIgWSD2hUBVAShUYak9l1jc3ezeefDIKE9fIjwiPLYs+04RjIwwN7u2 XrvXFsdPPnsDHOsDIi5bhJkQkcLvy/KVhE//upolYK0dGyYtuVIKjpdGPqeLR0SGIiWSDOUpq7gy ZszIpMmtO/dafssQEb1sGRyONWbqoWRdxqB1kdNxYnA6yeGYkpM9XosIItZlNjkcFXE6yeGYhuGg PN3onH9q6T7JtONu+A0zDwVLf5i3Tpq0NJyOPDHQsgVwONaJzFgpR0waz9QPzonMylZ+oSv9/eqI 7ThhIKLTSQ7HlMwyRq/s+F5mwA2lXSlV6jh5ON+dwzEZGc/V1L67JJ6lu+zKSGRLizdUS5nv54Hz 3d2zODvJ4ZiVGZeUVmQ8LRQDjxgb0uGoBWcnLZmVnSZnKBuGiOY7rcmXz2LO8VSJP73mP4s8I05f DU2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3p756Spp8thRvkd 9yYaSprsRH5kZzA55kRFw33eA9/YFp647BIxkh13FhEQOTIsli2jMBASIhIiIh8dWRJM/nfMfsm7 IibdC14YQKTQNlsE66KZ1kXOE4xO2nq6JvInOdJkJo9DP
The problem is that when i try to transform this code back in bitmap it doesn't work.

Related

Send an Image from c# server to Android client. Decoding issue

I want to send an image Bitmap from a c# server to an App in Android Java and I'm having a problem by decoding the file in java.
Te code in c# to decode an image into a string is as follows:
String bildString = ImageToString("C:\\Users\\Public\\Pictures\\Penguins.jpg");
public static string ImageToString(string path){
if (path == null)
throw new ArgumentNullException("path");
System.Drawing.Image im = System.Drawing.Image.FromFile(path);
MemoryStream ms = new MemoryStream();
im.Save(ms, im.RawFormat);
byte[] array = ms.ToArray();
return Convert.ToBase64String(array);
}
The string is transferred; and here comes the error in Java when I want to recover my image:
Bitmap bildAM = StringToBitMap(bildString);
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;
}
I receive following Exception:
StackTrace

How to get Base64 String from InputStream?

I'm on a problem by taking the selected gallery picture and want to save it first as Base64 String in a XML file (for later use. For example if you exit the app and open it again).
As you can see I get the Image on a InputStream
But first of all the onClick method:
public void onClick(DialogInterface dialog, int which) {
pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT);
pictureActionIntent.setType("image/*");
startActivityForResult(pictureActionIntent,GALLERY_PICTURE);
}
Now in the onActivityResult method I want to store the image from InputStream to Base64 String.
case GALLERY_PICTURE:
if (resultCode == RESULT_OK && null != data) {
InputStream inputstream = null;
try {
inputstream = getApplicationContext().getContentResolver().openInputStream(data.getData());
Base64InputStream in = new Base64InputStream(inputstream,0);
} catch (IOException e) {
e.printStackTrace();
}
#EDIT
This is what I do after creating the base64 String.
Bitmap bmp = base64EncodeDecode.decodeBase64(Items.get("image"));
Image1.setImageBitmap(bmp);
And this is the decoding Method:
public Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
I tried to use Base64InputStream but without success.
Can you give me a hint how to get from InputStream to Base64 String?
How many steps it will take doesn't matter.
I hope someone can help me!
Kind Regards!
Write these lines in onActivityResult method
try {
// get uri from Intent
Uri uri = data.getData();
// get bitmap from uri
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// store bitmap to file
File filename = new File(Environment.getExternalStorageDirectory(), "imageName.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);
out.flush();
out.close();
// get base64 string from file
String base64 = getStringImage(filename);
// use base64 for your next step.
} catch (IOException e) {
e.printStackTrace();
}
private String getStringImage(File file){
try {
FileInputStream fin = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
fin.read(imageBytes, 0, imageBytes.length);
fin.close();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
} catch (Exception ex) {
Log.e(tag, Log.getStackTraceString(ex));
toast("Image Size is Too High to upload.");
}
return null;
}
you can use base64 String of image.
Also don't forget to add permissions in AndroidManifest.xml file READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE
EDIT:: Decode base64 to bitmap
byte[] bytes = Base64.decode(base64.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
);
Hope it'll work.
This should work:
public static byte[] getBytes(Bitmap bitmap) {
try{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.flush();
//bitmap.compress(CompressFormat.PNG, 98, stream);
bitmap.compress(CompressFormat.JPEG, 98, stream);
//bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
return stream.toByteArray();
} catch (Exception e){
return new byte[0];
}
}
public static String getString(Bitmap bitmap){
byte [] ba = getBytes(bitmap);
String ba1= android.util.Base64.encodeToString(ba, android.util.Base64.DEFAULT);
return ba1;
}
Got this code from something I use in an application, stripped it down to the most basic as far as i know.
If you are selecting image from Gallery then why you are saving it as Base64 string in xml file , you can reuse that image from gallery .
For this save image url in SharedPreferences and use that url again to show image .
Edit :
If you want to store it locally then you can use SQLite Database to store it , for more detail visit this link .

How to get byte Image From url in Android

I am new to android.The Image is store in server by Base64 format. so how can i get it from server to My Project and set to my ImageView using Json Object.
Please Help me
Any help will be Appappreciated
Try this:
Convert Url to byte[] first:
byte[] bitmapdata = getByteArrayImage(url);
Method:
private byte[] getByteArrayImage(String url){
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
Now convert the byte[] to bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
And set your bitmap to your ImageView:
img= (ImageView) findViewById(R.id.imgView);
img.setImageBitmap(bitmap );
I found easy solution:
byte[] img = Base64.decode(userHeader.GetImage(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
imageww.setImageBitmap(getCircleBitmap(bitmap));
Using Apache's commons-io-2.5 lib we can get using this function IOUtils.toByteArray(is)
public static String getByteArrayFromURL(final String url) {
String base64Image = "";
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> result = executor.submit(new Callable<String>() {
public String call() throws Exception {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
return Base64.encodeToString(IOUtils.toByteArray(is), Base64.NO_WRAP);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
try {
base64Image = result.get();
} catch (Exception exception) {
exception.printStackTrace();
}
return base64Image;
}

BufferedImage into Android bitmap

I've got a problem. I need to save a java BufferedImage object in an String. Convert this String on the Android application into Bitmap. How can I achieve this? Or maybe you can recommend me the other way to transfer image information in the String format.
public static String encodeToString(BufferedImage image, String type) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
imageString = Base64.getEncoder().encodeToString(imageBytes);
bos.close();
} catch (IOException e) {
log.error("Can't encode to String");
}
return imageString;
}
Base64 encoding and decoding of images using Java 8:
public static String imgToBase64String(final RenderedImage img, final String formatName) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
public static BufferedImage base64StringToImg(final String base64String) {
try {
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64String)));
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
hope so will work,
enjoy your code:)

Use SD card for inputStream (images) android instead of internal memory

I'm trying to grab (with the method below) an image from the internet and do some canvas work with. but sometimes i'm having outOfMemory exception. So i'm wondering if is there a way to load the inputStream directly in the memory card instead of the internal memory.
private Bitmap LoadImageFromWebOperations(String url)
{
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
bitmap = ((BitmapDrawable)d).getBitmap().copy(Config.ARGB_8888, true);
return bitmap;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
the logcat says that the exception is due to that line :
Drawable d = Drawable.createFromStream(is, "src name");
Thx in advance!
I took this code from Fedor Vlasov's lazylist demo:
Lazy load of images in ListView.
First you need to create a function to copy your input stream to file output stream:
public static void CopyStream(InputStream is, OutputStream os)
{
final int buffer_size=1024;
try
{
byte[] bytes=new byte[buffer_size];
for(;;)
{
int count=is.read(bytes, 0, buffer_size);
if(count==-1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
Then get a cache folder:
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyCacheDir");
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
Then load your bitmap:
private Drawable getBitmap(String url)
{
String filename=URLEncoder.encode(url);
File f= new File(cacheDir, filename);
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
CopyStream(is, os);
os.close();
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
return new BitmapDrawable(bitmap);
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}

Categories

Resources