简体   繁体   English

如何使用共享首选项存储图像? 安卓系统

[英]How to store an image using sharedpreferences? Android

My code below put an image from gallery to my ImageButton But always when i leave the application or move to another activity the image don't save and the first background appear again. 我下面的代码将图库中的图像放到我的ImageButton中,但是总是在我离开应用程序或移至另一个活动时,图像不保存,并且第一个背景再次出现。

I need help, how can i save the image that i define to be my ImageButton background 我需要帮助,如何保存定义为ImageButton背景的图像

I read about sharedpreferences, but i don't know how to use on my app 我读到有关sharedpreferences的信息,但是我不知道如何在我的应用程序中使用

- --

- My CLASS - 我的课

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Adding the picture bit   

    imgButton = (ImageButton) findViewById(R.id.AddPic);
    imgButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
        }
    });
}
@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 SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
        SelectedCursor.moveToFirst();

        int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
        String picturePath = SelectedCursor.getString(columnIndex);
        SelectedCursor.close();

      //  Drawable d = new BitmapDrawable(getResources(),BitmapFactory.decodeFile(picturePath)); 
       // btnOpenGalery .setImageBitmap(d);
        imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();

    }

}   

my XML 我的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

<ImageButton
    android:id="@+id/AddPic"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:gravity="left"
    android:onClick="AddPic"
    android:background="@drawable/ic_launcher" />

</LinearLayout>

If you want to use sharedPreferences, use the below code: 如果要使用sharedPreferences,请使用以下代码:

  SharedPreferences sharedPreferences;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("data",  context.MODE_PRIVATE);

  //Adding the picture bit    

imgButton = (ImageButton) findViewById(R.id.AddPic);
imgButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) {
        Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
    } 
}); 

if(sharedPreferences!=null)
String path = sharedPreferences.getString("path", null);
if(path!=null)
  imgButton.setImageBitmap(BitmapFactory.decodeFile(path));




} 
@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 SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
    SelectedCursor.moveToFirst();

    int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
    String picturePath = SelectedCursor.getString(columnIndex);
    SelectedCursor.close();
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("path", picturePath);
    editor.commit();

  //  Drawable d = new BitmapDrawable(getResources(),BitmapFactory.decodeFile(picturePath));  
   // btnOpenGalery .setImageBitmap(d); 
    imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();

} 

} }

I did't do this task ealier but as I guess you can store image as Base64 string in Preferences . 我没有做这个任务,但是我想您可以将图像存储为Preferences中的Base64字符串。 When you want to get that image again then convert Base64 string to corresponding image . 当您想再次获取该图像时,请将Base64字符串转换为相应的图像。 You can follow this link to convert a image in Base64 string , and to convert a Base64 String to a image see this link 您可以单击链接将图像转换为Base64字符串,并将Base64字符串转换为图像,请参见此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM