如何在Android |共享首选项中保存图像 在Android中与图像共享偏好问题

在我的应用程序login后,我必须保存用户名和图像共享首选其他页面。 我能够优先保存名称,但不能获取任何地方如何保存图像。 请帮帮我。 我正在尝试类似的东西 –

SharedPreferences myPrefrence; String namePreferance="name"; String imagePreferance="image"; SharedPreferences.Editor editor = myPrefrence.edit(); editor.putString("namePreferance", itemNAme); editor.putString("imagePreferance", itemImagePreferance); editor.commit(); 

我想将图像保存为string后,将其转换为对象。 但是当我把它重新转换成位图时,我什么也没有得到

请帮我紧急我卡住了,花了2天的时间..

谢谢,

  • 圆angular与中风的位图图像
  • 如何在现有布局内的Java代码中创buildImageView?
  • 如何将图像转换为字节数组和字节数组为base64在android中的string?
  • Android OpenGL结合了SurfaceTexture(外部图像)和普通纹理
  • Android发布图片到Facebook墙上
  • 如何读取存储在内存中的图像文件?
  • 毕加索和PhotoView库加载图像到奇怪的ImageView
  • SoftReference是否在Bitmap对象上调用.recycle()
  • 我解决你的问题做这样的事情:

    1. 写入方法将您的位图编码为stringbase64-

       // 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; } 
    2. 在这个方法里面传递你的位图,就像你喜欢的东西一样:

       SharedPreferences.Editor editor = myPrefrence.edit(); editor.putString("namePreferance", itemNAme); editor.putString("imagePreferance", encodeTobase64(yourbitmap)); editor.commit(); 
    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); } 
    4. 请把你的string传给这个方法,然后做你想做的事情。

     Finally I solved this problem. 

    步骤:1.我在onCreate()中写了一些代码来获取以前Activity的intent数据

      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创buildsharedPreferences()并把这个代码

      private void sharedPreferences() { SharedPreferences shared = getSharedPreferences("App_settings", MODE_PRIVATE); SharedPreferences.Editor editor = shared.edit(); editor.putString("PRODUCT_PHOTO", encodeTobase64(bitmap)); editor.commit(); } 

    3创buildretrivesharedPreferences()这个方法并把这个代码,

     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()将位图编码为stringbase64-并将代码放入此方法的方法

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

    我希望这会对你有所帮助。

    编码到Base64? 这是疯狂的谈话! 这是太多的信息,你存储到共享偏好。 您应该做的策略是保存图像URI文件path,并检索它。 这样,您的应用程序将不会存储这么多的信息,并在解码图像时成为一个记忆猪。

    我在Github上做了一个简单的应用程序来演示这个想法,如果你想遵循:

    1.声明variables:

     private ImageView mImage; private Uri mImageUri; 

    2.select图像:

     public void imageSelect() { Intent intent; if (Build.VERSION.SDK_INT < 19) { intent = new Intent(Intent.ACTION_GET_CONTENT); } else { intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); } intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); } 

    3.保存图像URI:

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request we're responding to if (requestCode == PICK_IMAGE_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // The user picked a image. // The Intent's data Uri identifies which item was selected. if (data != null) { // This is the key line item, URI specifies the name of the data mImageUri = data.getData(); // Removes Uri Permission so that when you restart the device, it will be allowed to reload. this.grantUriPermission(this.getPackageName(), mImageUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); final int takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION; this.getContentResolver().takePersistableUriPermission(mImageUri, takeFlags); // Saves image URI as string to Default Shared Preferences SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit(); editor.putString("image", String.valueOf(mImageUri)); editor.commit(); // Sets the ImageView with the Image URI mImage.setImageURI(mImageUri); mImage.invalidate(); } } } } 

    4.需要时检索图像URI:

     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String mImageUri = preferences.getString("image", null); mImage.setImageURI(Uri.parse(mImageUri)); 

    而已! 现在我们已经将图像URIpath保存到共享首选项,并没有浪费宝贵的系统资源编码图像,并将其保存到SharedPreferences。

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