compress a bitmap and save it to a bitmap object - android

I am a beginner in android. I decoded a Base64 image to Bitmap and i need to compress it to to an Outputstream, now i need to use the Bitmap get added to the ArrayList.
try {
// Getting JSON Array from URL
details = json.getJSONArray(TAG_Root);
for(int i = 0; i < details.length(); i++){
JSONObject c = details.getJSONObject(i);
//Storing JSON item in a Variable
String branch = c.getString(TAG_Branch);
String address = c.getString(TAG_Add);
String uname = c.getString(TAG_User);
String photo = c.getString(TAG_Photo);
System.out.println(photo);
//decoding and compressing bitmap
byte[] imageAsBytes = Base64.decode(photo, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
//bitmap = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
//Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
al_branch.add(branch);
al_add.add(address);
al_user.add(uname);
al_photo.add(bitmap);
}
System.out.println(al_branch);
System.out.println(al_photo);
custom_list adapter = new custom_list(MainActivity.this, al_branch,al_user,al_add ,al_photo);

Related

Bytes Array is not Converting to Bitmap

Here is the image of response
When Services Giving the reposes of imges into bytes
bitmap is not converting bytes array
jsonObject= response.getJSONObject("dCafeMenuDisplayMainModel");
jsonArray = jsonObject.getJSONArray("cafeMenuDayList");
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObjectInner = jsonArray.getJSONObject(i);
//list.add(new cafe_menu("Hellow","Yo Man",""));
String title = jsonObjectInner.getString("cafeMenuTitle");
String bytes = jsonObjectInner.getString("cafeMenuImage");
byte[] data = bytes.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

How do I load an image in android

How do I load an image from my database to my android application and put it in a listview. The database is MySQL and the image is stored in png format
Here is my codes for retrieving data in my database. The a_emblem is the imageview and the image in my json file
private void showResult() {
JSONObject jsonObject;
ArrayList<HashMap<String, String>> list = new ArrayList<>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY1);
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
String a_shortcut = jo.getString(Config.TAG_a_shortcut);
String a_emblem = jo.getString(Config.TAG_a_emblem);
String gold = jo.getString(Config.TAG_gold);
String silver = jo.getString(Config.TAG_silver);
String bronze = jo.getString(Config.TAG_bronze);
String total = jo.getString(Config.TAG_total);
HashMap<String, String> match = new HashMap<>();
match.put(Config.TAG_a_shortcut, a_shortcut);
match.put(Config.TAG_a_emblem, a_emblem);
match.put(Config.TAG_gold, gold);
match.put(Config.TAG_silver, silver);
match.put(Config.TAG_bronze, bronze);
match.put(Config.TAG_total, total);
list.add(match);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
getActivity(), list, R.layout.standlayout,
new String[]{Config.TAG_a_shortcut, Config.TAG_a_emblem, Config.TAG_gold, Config.TAG_silver, Config.TAG_bronze, Config.TAG_total},
new int[]{R.id.shortcut, R.id.img, R.id.gold, R.id.silver, R.id.bronze, R.id.total});
lv.setAdapter(adapter);
}
use Picasso or glide here is the url for you
Picasso
or
Glide
Here is what you can do.
Convert your image to a bitmap.
Convert your image to a base64 string and save this base64 string in the database.
Convert the base64 string back to the image while using it in the adapter.
Set bitmap in ImageView.
It should definitely work. Code for reference
Convert drawable to Bitmap
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
Use the following method to convert a bitmap to a byte array:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
to encode a base64 string from a byte array:
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
save encoded in the database.
Convert the base64 string back to Bitmap:
byte[] decodedString = Base64.decode(encoded , Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Set the bitmap to the image view:
imageView.setImageBitmap(bitmap);

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.getBitmap();
encodedImage = encodeToBase64(bitmap, Bitmap.CompressFormat.JPEG, 100);
I get the following as String
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCADmAZgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhY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jAaaS0DbGjUOw2veTbIAUIKpPuUAxsTL4at40jmFxGsgtVa+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/n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1A1wFaHxbgFShenh5b6Y+RfZktdPSmkFwMXZYlwV1zTVY8kCug0lQxj974Txf8+krbtSfg+qB6ADjluw2LmMjQ+ghDga9vPF9UOx3P+7si33A38fqgegEx4hMLbzELoNBQPDDwYCEICTNqySwX0I0W3oKNyfr7m58p0ypPWSAgFogCMEvCLoJQNdgzJ3G3JnfGH4q2D44UAAYmByjKBs3Yaw1E8GBCAhpmYNbO82hKh+OiAAKXGak/aSQXECG7sNsX9fqdAa05px6gIEICOOe9BLdfdA+1WBDalCZ7Zf2dzctBLL/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);

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