重启Android后,如何确保用户选择的图像不会改变?

我有一个简单的贴纸小部件,允许用户在放置小部件时选择其中一个贴纸。 我的问题是,一旦设备重新启动,贴纸将恢复为默认设置。 无论设备是否重新启动,如何确保用户选择的标签仍然存在? 一个例子或推动正确的方向将是非常有帮助的。 我不是在寻找任何人为我做这件事,只是为了接受如何去做这件事的教育。 谢谢。

编辑 包含的代码

我不确定,但我认为我错了。 我不知道如何以我现在拥有的方式合并SharedPreferences。 有人能给我一个例子说明我应该如何使用至少一张我正在使用的图片吗?

public class SpraycanConfig extends Activity { int xawID; AppWidgetManager xawm; Context xc; EditText xinfo; //EasyTracker easyTracker; protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); Crashlytics.start(this); setContentView(R.layout.scconfiglayout); this.xc = this; Bundle localBundle = getIntent().getExtras(); if (localBundle != null) { this.xawID = localBundle.getInt("appWidgetId", 0); } for (;;) { this.xawm = AppWidgetManager.getInstance(this.xc); ImageButton localImageButton1 = (ImageButton)findViewById(R.id.normal); View.OnClickListener local1 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.normal); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton1.setOnClickListener(local1); ImageButton localImageButton2 = (ImageButton)findViewById(R.id.belton); View.OnClickListener local2 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.belton); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton2.setOnClickListener(local2); ImageButton localImageButton3 = (ImageButton)findViewById(R.id.gold); View.OnClickListener local3 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.gold); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton3.setOnClickListener(local3); ImageButton localImageButton4 = (ImageButton)findViewById(R.id.hardcore); View.OnClickListener local4 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.hardcore); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton4.setOnClickListener(local4); ImageButton localImageButton5 = (ImageButton)findViewById(R.id.ironlak); View.OnClickListener local5 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.ironlak); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton5.setOnClickListener(local5); ImageButton localImageButton6 = (ImageButton)findViewById(R.id.krylon); View.OnClickListener local6 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.krylon); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton6.setOnClickListener(local6); ImageButton localImageButton7 = (ImageButton)findViewById(R.id.mtn94); View.OnClickListener local7 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.ninetyfour); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton7.setOnClickListener(local7); ImageButton localImageButton8 = (ImageButton)findViewById(R.id.rusto); View.OnClickListener local8 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main); localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.rusto); xawm.updateAppWidget(xawID, localRemoteViews); Intent localIntent = new Intent(); localIntent.putExtra("appWidgetId", xawID); setResult(-1, localIntent); finish(); } }; localImageButton8.setOnClickListener(local8); return; } } @Override protected void onStart() { super.onStart(); } @Override protected void onStop() { super.onStop(); } } 

完全是这样的:

 protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); SharedPreferences sharedPreferences = getSharedPreferences("myPref", 0); // get the last chosen sticker int sticker = sharedPreferences.getInt("chosenSticker", R.drawable.normal); //the .normal is the default value in case the user didn't choose one. myView.setImageViewResource(R.id.imageview, sticker); //now you can use it // store user's choice for example: View.OnClickListener local2 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("chosenSticker", R.drawable.belton); editor.apply(); // your rest of your code here ... } } } 

SharedPreferences将是您的最佳选择。

实际上你可以用android中使用的任何存储方式保存所选图像。

我最喜欢的方法是使用SharedPreferences 。

这是你如何去做的:

1)初始化SharedPreferences对象。 如果要存储影响应用程序不同方面的值,请使用标记进行分类。

 SharedPreferences myPrefs = getSharedPreferences("Stickers", MODE_PRIVATE); 

2)检索与其关联的值。 在您的情况下,它可以是URL或资产ID。 该函数需要第二个值,如果尚未设置首选项,则返回该值。

 myPrefs.getString("SelectedSticker", "default.jpg"); 

3)存储新值或替换现有值。

 public void setStickerPreference(String stickerURL) { // retrieve editor SharedPreferences.Editor myPrefsEditor = myPrefs.edit(); // save or modify the field myPrefsEditor.putString("SelectedSticker", stickerURL); // commit changes prefsEditor.commit(); } 

希望这可以帮助!

无论用户是否真的改变了,都要坚持你的形象或你可以去了解的逻辑。

关于将共享偏好放在哪里的问题,把它放在改变图像的代码函数或你所说的内容中,你可以使用I / O写入临时存储,让我们说上下文.getCacheDir()你可以保留你的图像然后在你的onResume()中也检查File.exist()是否存在该文件

还记得在完成访问后删除它。