如何将从图库中挑选的图像保存到共享首选项

时间:2016-01-07 15:44:11

标签: android image gallery android-sharedpreferences

我制作了一个具有背景图像集的应用程序。现在我想从图库中选择一个图像并将其设置为我的应用程序背景。这部分完成了。但我也希望这个挑选的图像永久地设置到我的应用程序背景,因为当我重新打开我的应用程序时,默认图像被设置。

如何永久保存所选图像,直到我再次尝试更改?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bn= (Button) findViewById(R.id.button);
    SharedPreferences sp = getSharedPreferences("student", MODE_PRIVATE);
    final SharedPreferences.Editor spedit = sp.edit();
    v = R.drawable.back;
    spedit.putInt("background", v);
    RelativeLayout bg = (RelativeLayout) findViewById(R.id.abc);
    bg.setBackgroundResource(v);

    bn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, 101);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 101 && 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();
        bitmap = BitmapFactory.decodeFile(picturePath);
        Drawable d = new BitmapDrawable(getResources(),bitmap);
        RelativeLayout bg = (RelativeLayout) findViewById(R.id.abc);
        bg.setBackground(d);
    }
}

我希望当应用程序第一次运行时会看到默认背景,并且当我更改背景时,应该在进一步启动时看到图像。

2 个答案:

答案 0 :(得分:1)

在下面我们将我们的图片地址分配给共享首选项。之后在oncreate部分我们检查了地址是否有效。根据我们更改了背景。

 SharedPreferences sp; 
 @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bn= (Button) findViewById(R.id.button);
            sp = getSharedPreferences("student", MODE_PRIVATE); 
             String savedPicturePath = sp.getString("imagepath","null");
             if(!savedPicturePath.equals("null"){
             bitmap = BitmapFactory.decodeFile(savedPicturePath);
                Drawable d = new BitmapDrawable(getResources(),bitmap);
                RelativeLayout bg = (RelativeLayout) findViewById(R.id.abc);
                bg.setBackground(d);
            }
            bn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(i, 101);

                }
            });


        }

            @Override
                  protected void onActivityResult(int requestCode, int resultCode,    Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == 101 && 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); 
                sp.edit().putString("imagepath",picturePath);
                cursor.close();
                bitmap = BitmapFactory.decodeFile(picturePath);
                Drawable d = new BitmapDrawable(getResources(),bitmap);
                RelativeLayout bg = (RelativeLayout) findViewById(R.id.abc);
                bg.setBackground(d);


            }


        }

        }

答案 1 :(得分:-1)

当用户点击图片时,在onActivityResult中,获取图片路径并将其保存在SharedPreferences中。下次获取保存的值并将后台资源设置为该值。简单。 提示:您获得的错误是因为您尚未声明SharedPreferences。 您可以在下面声明: SharedPreferences a = context.getSharedPreferences(“prefstring”,MODE_PRIVATE);