1

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

在我尝试再次更改之前,如何永久保存所选图像?

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

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

4

2 回答 2

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


            }


        }

        }
于 2016-01-07T16:01:45.063 回答
-1

当用户单击图像时,在 onActivityResult 中,获取图像路径并将其保存在 SharedPreferences 中。下次获取保存的值并将背景资源设置为该值。简单的。提示:你得到的错误是因为你没有声明 SharedPreferences。您可以像下面这样声明它: SharedPreferences a = context.getSharedPreferences ("prefstring" , MODE_PRIVATE);

于 2016-01-07T19:08:12.077 回答