Finally I solved this problem.
步骤:- 1. 我在 onCreate() 中编写了一些代码,用于从之前的 Activity 中获取意图数据
private Bitmap bitmap;
Intent intent = getIntent();
if (getIntent().getExtras() != null)
{
// for get data from intent
bitmap = intent.getParcelableExtra("PRODUCT_PHOTO");
// for set this picture to imageview
your_imageView.setImageBitmap(bitmap);
sharedPreferences();
}else
{
retrivesharedPreferences();
}
2 创建 sharedPreferences() 并把这段代码
private void sharedPreferences()
{
SharedPreferences shared = getSharedPreferences("App_settings", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("PRODUCT_PHOTO", encodeTobase64(bitmap));
editor.commit();
}
3创建retrivesharedPreferences()这个方法并放上这段代码,
private void retrivesharedPreferences()
{
SharedPreferences shared = getSharedPreferences("MyApp_Settings", MODE_PRIVATE);
String photo = shared.getString("PRODUCT_PHOTO", "photo");
assert photo != null;
if(!photo.equals("photo"))
{
byte[] b = Base64.decode(photo, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(b);
bitmap = BitmapFactory.decodeStream(is);
your_imageview.setImageBitmap(bitmap);
}
}
4 编写 encodeTobase64() 方法将您的位图编码为字符串 base64- 并将代码放入此方法中
public static String encodeTobase64(Bitmap image) {
Bitmap bitmap_image = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap_image.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
我希望它对你有帮助。