How to save Image in shared preference in Android | Shared preference issue in Android with Image

前端 未结 4 642
难免孤独
难免孤独 2020-11-27 17:34

In my application after login I have to save user name and image in shared preference for other pages. I am able to save name in preference but can\'t get any where how to s

4条回答
  •  [愿得一人]
    2020-11-27 17:36

    //Thanks Maish srivastava 
    // i will do complete code just copy past and run it sure worked it
    //
    public class MainActivity extends Activity  implements OnClickListener
    {
        private static int RESULT_LOAD_IMAGE = 1;
        public static final String MyPREFERENCES = "MyPre" ;//file name
           public static final String  key = "nameKey"; 
           SharedPreferences sharedpreferences ;
           Bitmap btmap;
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
             sharedpreferences =  getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
             if (sharedpreferences.contains(key))
              {
                    String u=sharedpreferences.getString(key, "");
                    btmap=decodeBase64(u); 
                    ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    iv.setImageBitmap(btmap);
              }
           ImageButton imgbtn=(ImageButton) findViewById(R.id.imageButton1);
           imgbtn.setOnClickListener(this);
        }
    
    public void onClick(View  v) 
    {
        // TODO Auto-generated method stub
        switch (v.getId())
         {
                 case R.id.imageButton1: 
                      try 
                      { //go to image library and pick the image
                       Intent i=newIntent(ntent.ACTION_PICK,
                       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                       startActivityForResult(i, RESULT_LOAD_IMAGE);      
                      } 
                      catch (Exception e) 
                      {
                       e.printStackTrace();
                      }
                      break;
         }  
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 
        {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
    
            ImageView iv = (ImageView) findViewById(R.id.imageView1);
            iv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            btmap=BitmapFactory.decodeFile(picturePath);//decode method called
    
            Editor editor = sharedpreferences.edit();
            editor.putString(key,  encodeTobase64(btmap));
            editor.commit();   
        }
    
    
    }
    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);
    }
    @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }
    

提交回复
热议问题