This question already has answers here:
How to pass image data from one activity to another activity?
(9 answers)
Closed 5 years ago.
I have to transfer the image from one activity to another. In first activity there are two buttons (take photo) and (View Image).User can take photo by pressing (take photo) button and that photo will be transfer to another activity class which can be view by (View Image) button. Help required.
There is another way for doing this. You can convert the Bitmap image and convert into Base 64 and store it in shared preference. In another activity you can reconvert back it into bitmap and use where ever you want.
Bitmap Conversion and Storing into Shared Preference
Bitmap photo = (Bitmap) intent.getParcelableExtra("Your data");
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();
}
Related
how can I save an ImageView in sharedpreferences?
I am trying to create a quiz where the player needs coins to unlock the next level, so the next level will be with a lock, and as soon as the player buys the level, the lock goes away, I already got the score. save, now only the image is missing, thanks in advance everyone!
solved your problem do something like that:
Write Method to encode your bitmap into string base64-
// method for bitmap to base64
public static String encodeTobase64(Bitmap image) {
Bitmap immage = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Log.d("Image Log:", imageEncoded);
ret
urn imageEncoded;
}
2.Pass your bitmap inside this method like something in your preference:
SharedPreferences.Editor editor = myPrefrence.edit();
editor.putString("namePreferance", itemNAme);
editor.putString("imagePreferance", encodeTobase64(yourbitmap));
editor.commit();
3 And when you want to display your image just anywhere, convert it into a bitmap again using the decode method:
// method for base64 to bitmap
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
Please pass your string inside this method and do what you want.
Get bitmap from the imageview then convert into base64 string then save it to the sharedpreference then again when you need the image get the base64 string convert to bitmap then user imageview.setBitmap(bitmap);
Now You are all set
For encoding and decoding bitmap you can use this, :
public static String encodeTobase64(Bitmap image) {
Bitmap immage = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Log.d("Image Log:", imageEncoded);
return imageEncoded;
}
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
It isn't a good idea to store your images in shared preferences because shared preferences are used for storing app settings that are lightweight. You should use an SQLite or Room database especially if you have many images. Another way is to cache your images in external storage.
This question already has an answer here:
How to convert a image into Base64 string without compressing the image?
(1 answer)
Closed 3 years ago.
I am trying to convert a bitmap image into base64 String using this code. I and getting a very low-quality image. How can I get a good quality image after convert bitmap into Base64 Sring
public String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
base64Image = Base64.encodeToString(b, Base64.DEFAULT);
return base64Image;
}
If I have a 5MB image. After convert, I am getting the only 160KB image. But in my case, I don't want to compress my image too much I just want to get Base64 String based on Bitmap image only JPEG format, not any other. Please help me with this.
Try this:
public String getBase64Image(Bitmap bitmap) {
try {
ByteBuffer buffer =
ByteBuffer.allocate(bitmap.getRowBytes() *
bitmap.getHeight());
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();
return Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I am trying to convert an image stored in Database in Base64 format, to a Bitmap to be used in an Imageview.
So, I store it in SQLite this way:
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap fotoGrande=(Bitmap) extras.get("data");
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
//I am adding some data to EXIF here, add to an static ArrayList<Bitmap> in other class and I store it this way:
int bytes=listaFotos.get(i).getFoto().getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
values.put("foto", Base64.encodeToString(buffer.array(), Base64.DEFAULT));
Later, i need to get that image to fit it in an ImageView:
String foto = csr2.getString(0);//csr2 is a cursor
byte[] arrayFoto = Base64.decode(foto, Base64.DEFAULT);//This is not null
Bitmap fotoBitmap = BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);//This is null
I know there are tons of questions about this. I searched, but no answer fix my problem.
Why is my BitmapFactory.decodeByteArray returning null? What I am doing wrong? Any help?
Thank you.
Turned out to be a database issue. SQLite gives you a cursor of 1MB MAX size. I was getting the bytes from database, with a 1MB cursor, the pic was not sent properly.
To fix it, I stored the path to the photo in database instead of the bytes.
firstly image is convert in Bitmap to String like this
ByteArrayOutputStream stream = new ByteArrayOutputStream();
camera.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
String encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
where camera is a Bitmap.
and store the String encodedImage in database
And getImage string like this
byte[] b = Base64.decode(encodedImage , Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
This question already has answers here:
How to store and retrieve bitmap in sharedPreferences in Android?
(4 answers)
Closed 9 years ago.
I want to store the bitmap object in shared preferences and on resume method just retrieve that object and set it in background.Please tell me how to store and retrieve it form shared preferences.The problem is that in shared preferences we can put the values like String,int,bolean,long etc but not the bitmao object.Please help me to sort out this problem.Below is my code:
#Override
protected void onResume() {
super.onResume();
rl_changeBackground.setBackgroundDrawable(new BitmapDrawable(getResources(),HomeSafeStaticVariables.bitmap));
}
}
You can add only Boolean, Float, Int, Long, String values in SharedPreference. But one thing you can do is converting Bitmap to Base64 String. And after retrieving it from SharedPrefrence convert it to Bitmap.
Use following method to convert bitmap to byte array:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
to encode base64 from byte array use following method
String encoded = Base64.encodeToString(b, Base64.DEFAULT);
And Save it to SharedPrefrence.
Now assuming that your image data is in a String called encoded , the following should do give you BitMap from Base64 string:
byte[] imageAsBytes = Base64.decode(encoded.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
This may help you. Try and please let me know !
you can store Bitmap as base64 string in SharedPreferences.But it is not good practice to store bitmap images in SharedPreferences. you should store the image in the SD card and save the path in the SharedPreferences.
Check this question
If you really want to store your image into SharedPreferences you should have a look at this solution (or similar that already exist here):
How to store and retrieve bitmap in sharedPreferences in Android?
My situation is that: I have a button that change background of my parent layout with a image of SD (this work fine). Then I like save these image in a SharedPreference to allow the user start my app with their background image, and not my default background image. I save the image this way:
SharedPreferences.Editor editor = prefs.edit();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
editor.putString("background", Base64.encodeToString(b, Base64.DEFAULT ));
And I retrieve on this way (this code is in onCreate):
prefs = getSharedPreferences("Mis preferencias",Context.MODE_PRIVATE);
String fondo = prefs.getString("background", "vacio");
if(!fondo.equals("vacio")){
byte[] b = Base64.decode(fondo, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(b);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(is);
BitmapDrawable bd = new BitmapDrawable(getResources(), yourSelectedImage);
View view = findViewById(R.id.padre);
view.setBackgroundDrawable(bd);
}
Is the first time that use sharedpreferences and play with images in base64, so I'm little stuck with this, if I kill my app and restart, the default backgrounds appear, instead the custom. Any help? thanks and sorry for my english.
You forgot editor.commit() to actually save you string in Preferences.