我制作了一个设置了背景图像的应用程序。现在我想从图库中选择一张图片并将其设置为我的应用程序背景。这部分完成了。但我也希望这个选择的图像永久设置为我的应用程序背景,因为当我重新打开我的应用程序时,默认图像被设置。
在我尝试再次更改之前,如何永久保存所选图像?
@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);
}
}
我希望当应用程序第一次运行时可以看到默认背景,并且当我更改背景时,应该在进一步启动时看到该图像。