How to store imageview after picking it from gallery - android

I'm making a user profile which the user can change his profile picture, but I want the selected image to be stored and not to disappear after I go to another activity, I already did the selecting image from the gallery part. I know this has something to do with shared preferences or bitmap coding but I can't seem to figure out how to do it.
How can I do that exactly, and thank you.

First covert the Image Path you get into Base64 String using this function
public static String getFileToByte(String path){
Bitmap bm = null;
ByteArrayOutputStream baos = null;
byte[] b = null;
String encodeString = null;
try{
bm = BitmapFactory.decodeFile(path);
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
b = baos.toByteArray();
encodeString = Base64.encodeToString(b, Base64.DEFAULT);
}catch (Exception e){
e.printStackTrace();
}
return encodeString;
}
Save the Base64 in SharedPreferences
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("image_data",getFileToByte("/path/to/image.jpg"));
edit.commit();
Show it back in IMAGEVIEW when needed
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
String previouslyEncodedImage = shre.getString("image_data", "");
if( !previouslyEncodedImage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
imageView.setImageBitmap(bitmap);
}

Related

how to save image in the activity from galley?

I am recently trying to save image and text with sharedpreferences, but saving image does not go well.
this is the main activity code :
imageView = (ImageView) findViewById(R.id.imageview);
Button uploading = (Button) findViewById(R.id.upload_upload);
uploading.setOnClickListener(new View.OnClickListener() { // UPLOAD BUTTON
#Override
public void onClick(View view) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
float scale = (float) (1024/(float)bitmap.getWidth());
int image_w = (int) (bitmap.getWidth() * scale);
int image_h = (int) (bitmap.getHeight() * scale);
Bitmap resize = Bitmap.createScaledBitmap(bitmap, image_w, image_h, true);
resize.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(upload.this,MainActivity.class);
intent.putExtra("picture", byteArray);
intent.putExtra("integer", 300);
intent.putExtra("double", 3.141592);
intent.putExtra("image", byteArray);
and this is the subactivity which sets image.
imageView = (ImageView) findViewById(R.id.main_image);
byte[] byteArray = getIntent().getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageView.setImageBitmap(bitmap);
you need to convert image into Base64 string representation by using below code:
String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("image_data",encodedImage);
edit.commit();
and then, when retrieving, convert it back into bitmap:
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
String previouslyEncodedImage = shre.getString("image_data", "");
if( !previouslyEncodedImage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
imageConvertResult.setImageBitmap(bitmap);
}

How to convert small icon from notification using NotificationListenerService to byte[]?

I have an app in which I read all the incoming notification from device. I am facing problem in converting small icon into byte[]. How do I do that please help ?
code:-
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
//use :
Context remotePackageContext = null;
Bitmap bmp = null;
try {
remotePackageContext = getApplicationContext().createPackageContext(pack, 0);
Drawable icon = remotePackageContext.getResources().getDrawable(your_id);
if(icon !=null) {
bmp = ((BitmapDrawable) icon).getBitmap();
}
} catch (Exception e) {
e.printStackTrace();
}
//now convert this bitmap to byte :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 90, baos);
byte[] b = baos.toByteArray();

Converting Bitmap into base64 string and saving in Shared Preference

In my onCreate method, i have a bitmap image (transferred from another activity).
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
Below my onCreate method, i wrote a method to convert my bitmap into base64 string.
public static String encodeToBase64(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Log.d("Image log:", imageEncoded);
return imageEncoded;
};
Finally, my method to save in shared preferences:
public void savepic (View view){
SharedPreferences mypreference = getSharedPreferences("image", Context.MODE_PRIVATE);
String Pic1 = "image1";
SharedPreferences.Editor editor = mypreference.edit();
editor.putString("Pic1",encodeToBase64(bitmap));
editor.commit();
};
However, in the following line below, it can't seems to read my bitmap variable (cannot resolve symbol bitmap). i really habe no idea what to do... Any help is greatly appreciated~
editor.putString("Pic1",encodeToBase64(bitmap));
I have done the same thing in my project.
Bitmap Conversion and Storing into Shared Preference
Bitmap photo = (Bitmap) intent.getParcelableExtra("BitmapClothes");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
myPrefsEdit.putString("url", temp);
myPrefsEdit.commit();
Retrieving from Shared Preference and Loading it into an ImageView
String temp = myPrefs.getString("url", "defaultString");
try {
byte[] encodeByte = Base64.decode(temp, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
picture.setImageBitmap(bitmap);
} catch (Exception e) {
e.getMessage();
}

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();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Xk0tk9cH9iPRGku/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+rBlBkIQMF4Zvj5VB6Dj4IFYT1hhVA4EIACEEbPxt5DdaOfpno8mrOTjorBetWDsR0IgCQco3evCarHYyj7qC4G6yEGcoAA5IizvF9MMHoZuGKwHG5CfkAAMu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LL/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();
}
}

Categories

Resources