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);
}
Related
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);
}
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();
}
I'm trying to draw a Bitmap on a ImageView, but it is not showing... I created the Bitmap and stored on a Intent Extra, but on the new activity I'm not able to draw it on the ImageView.
Here is my code, calling the new activity:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
myIntent.putExtra("createdImg", b);
startActivity(myIntent);
}
And on the new activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
backgroundImg = (ImageView)findViewById(R.id.imageView1);
Intent myIntent = getIntent();
Bitmap bitmap = (Bitmap) myIntent.getParcelableExtra("createdImg");
backgroundImg.setImageBitmap(bitmap);
}
What am I missing? Thanks!
Try to send it as ByteArray and decode it in the receiving activity.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Share.class);
in1.putExtra("image",byteArray);
in the second activity
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
You can also try like this
Bundle bundle = new Bundle();
bundle.putParcelable("createdImg", b);
intent.putExtras(bundle);
and in next activity
Bundle bundle = this.getIntent().getExtras();
Bitmap bmap = bundle.getParcelable("createdImg");
as given in this tutorial http://android-er.blogspot.com/2011/10/pass-bitmap-between-activities.html
I took it a little bit from every answer, plus other researches, and I came up with the code below that worked nicely:
Calling the new activity:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
// create bitmap screen capture
Bitmap bitmap;
View v1 = v.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
// create a new one with the area I want
Bitmap bmpLast = Bitmap.createBitmap(bitmap, 25, 185, 430, 520);
v1.setDrawingCacheEnabled(false);
//create an empty bitmap with the size I need
Bitmap bmOverlay = Bitmap.createBitmap(480, 760, Bitmap.Config.ARGB_8888);
//draw the content on the bitmap
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmpLast, 0, 0, null);
//compress the bitmap to send to other activity
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmpLast.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//store compressed bitmap
myIntent.putExtra("createdImg", byteArray);
startActivity(myIntent);
}
And on the new activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
//decompress the bitmap
byte[] byteArray = getIntent().getByteArrayExtra("createdImg");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//set the imageview with the bitmap
backgroundImg = (ImageView)findViewById(R.id.imageView1);
backgroundImg.setImageDrawable(new BitmapDrawable(getResources(), bmp));
}
Thanks everyone! =)
I am working with an image. In my application I have displayed an image from drawable and set that drawable image to ImageView. When I click on a button I would like to encode the ImageView image to byte code by using Base64.
I have implemented code as follows:
((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.person);
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((TextView)findViewById(R.id.textView1)).setText("Get((ImageView)findViewById(R.id.imageView1)) image Base64.encode() here");
}
});
How can I get the encoded imageView1 image to byte code?
Can anybody please help me.
Try this...
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.images);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
System.out.println("byte array:"+image);
String img_str = Base64.encodeToString(image, 0);
System.out.println("string:"+img_str);
Now set that string to your Textview as
tv.setText(img_str);
Look at this code,
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.person)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bMap .compress(Bitmap.CompressFormat.PNG, 100, baos);
//bMap is the bitmap object
byte[] b = baos.toByteArray();
String encodedString = Base64.encodeToString(b, Base64.DEFAULT)
use this
public String encode(Bitmap icon) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 50, baos);
byte[] data = baos.toByteArray();
String test = Base64.encodeBytes(data);
return test;
}`
how can i store an icon into Blob?
anyone please help me on this topic
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
Blob b;
I have try this ::
button1 = (Button)findViewById(R.id.button1);
try{
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
ImageView iv2 = (ImageView)findViewById(R.id.imageView1);
Drawable d =iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
System.out.println(".....d....."+d);
System.out.println("...bitDw...."+bitDw);
System.out.println("....bitmap...."+bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
System.out.println("imageInByte"+imageInByte);
String s = imageInByte.toString();
byte[] imageInByte2 = s.getBytes();
Bitmap btmp = convertByteArrayToBitmap(imageInByte2);
/*iv.setImageResource(R.drawable.icon);*/
iv2.setImageBitmap(btmp);
}catch (Exception e) {
e.printStackTrace();
}
// i1.insertDataUser("tableName",3,"appname","pname", "versionName", 2,"s", 2121);
/* int a = i1.GetUserData(getApplicationContext(), "tablename",1);
System.out.println("a is "+a);
System.out.println(" i1.app_id"+i1.app_name);
System.out.println(" i1.app_id"+i1.pname);
System.out.println(" i1.app_id"+i1.version_name);
System.out.println(" i1.app_id"+i1.versionCode);
System.out.println(" i1.app_id"+i1.date);
System.out.println(" i1.app_id"+i1.icon);*/
}
public static Bitmap convertByteArrayToBitmap(
byte[] byteArrayToBeCOnvertedIntoBitMap) {
Bitmap bitMapImage = BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
return bitMapImage;
}
I see you convert from Drawable to Bitmap anyway, so you might use this:
import android.util.Base64;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;
//use this method
private byte[] getBase64Bytes(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encode(byteArray, Base64.DEFAULT);
}
Convert from Bitmap to byteArray. You can store this as a blob in your database:
Bitmap myBitmap; //should be initialized to some value
byte[] byteArray = getBase64Bytes(myBitmap);
For the sake of completeness, how to convert from byteArray to Bitmap again
public Bitmap getImageFromBase64Blob(byte[] blob){
byte[] byteArray = Base64.decode(blob, Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);
return bitmap;
}