如何在Android中以共享首选项保存图像 Android中使用Image的共享首选项问题

12 android image sharedpreferences

在登录后的我的应用程序中,我必须以其他页面的共享首选项保存用户名和图像.我可以优先保存名称,但无法获得如何保存图像.

我正在尝试这样的事情 -

SharedPreferences myPrefrence;
    String namePreferance="name";

    String imagePreferance="image";

SharedPreferences.Editor editor = myPrefrence.edit();
                editor.putString("namePreferance", itemNAme);
                editor.putString("imagePreferance", itemImagePreferance);
                editor.commit();
Run Code Online (Sandbox Code Playgroud)

我试图将图像转换为对象后保存为字符串.但当我将其重新转换为位图时,我没有得到任何东西.

Man*_*ava 45

我解决了你的问题做了类似的事情:

  1. Write方法将您的位图编码为字符串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);
        return imageEncoded;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将您的位图传递到此方法中,就像您喜欢的那样:

    SharedPreferences.Editor editor = myPrefrence.edit();
    editor.putString("namePreferance", itemNAme);
    editor.putString("imagePreferance", encodeTobase64(yourbitmap));
    editor.commit();
    
    Run Code Online (Sandbox Code Playgroud)
  3. 当您想在任何地方显示图像时,请使用解码方法将其再次转换为位图:

    // method for base64 to bitmap
    public static Bitmap decodeBase64(String input) {
        byte[] decodedByte = Base64.decode(input, 0);
        return BitmapFactory
                .decodeByteArray(decodedByte, 0, decodedByte.length);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 请在此方法中传递您的字符串并执行您想要的操作.


sac*_*are 6

Finally I solved this problem.
Run Code Online (Sandbox Code Playgroud)

步骤:- 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();
        }
Run Code Online (Sandbox Code Playgroud)

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();
    }
Run Code Online (Sandbox Code Playgroud)

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);
        }

    }
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

我希望它对你有帮助。