我已经问过,但我的理解略有增加。我已经想出了如何让用户在布局上选择自定义背景图片。我用这个:如何使背景图像持久
在我的onCreate方法:
Button player = (Button) setBg.findViewById(R.id.plBg);
player.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setDataAndType(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
startActivityForResult(i, RESULT_LOAD_PLAYER);
setBg.dismiss();
}
});
,并在我的onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_PLAYER && 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();
playerBg = (ImageView) findViewById(R.id.playerBg);
playerBg.setImageBitmap(BitmapFactory.decodeFile(picturePath));
playerBg.setScaleType(ScaleType.CENTER_CROP);
}
}
但是当你回来'出来的应用程序,背景将恢复为默认值。我如何获得该背景选择?
我已经看这个:Save bitmap to location
但我有一个很难理解它。
我也尝试过将它保存在共享偏好中,但了解到这不是它们的用途。 我还是这个初学者。提前致谢。
上面的代码是从什么方法来的? –
对不起,这是在我的onActivityResult。我将编辑OP以使其更清晰 – Psest328
为什么不在sharedpreference中有一个标志,表示用户设置了任何背景?而且,如果用户设置了任何背景,则可以将图片路径保存在sharedpreference中。所以,每次你的活动开始时。您从sharedpref检查标志,并使用保存在共享前缀中的图片路径设置背景。 – VendettaDroid