我有一个活动,从你的画廊拍摄照片并将它们放在屏幕上,但是当我退出应用程序并返回时,它们不会保存在那里。当我退出并回来时,我如何将它们保存在活动中? 一直在尝试onSavedInstanceState但我不知道我在做什么了。我有点新鲜。这是我的活动:
public class HatActivity extends Activity {
private static final int RESULT_LOAD_IMAGE = 1;
LinearLayout main;
Bitmap b;
int count = 0;
@Override
public void onSaveInstanceState(Bundle toSave) {
super.onSaveInstanceState(toSave);
toSave.putParcelable("bitmap", b);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) b = savedInstanceState.getParcelable("bitmap");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hat);
main = (LinearLayout) findViewById(R.id.main);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar);
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.abc_textfield_search_right_default_holo_light));
getActionBar().setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.abc_search_url_text_normal)));
}
public void infoClick(View view) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && 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();
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final ImageView iv = new ImageView(this);
lparams.gravity=0x11;
lparams.topMargin=60;
lparams.bottomMargin=30;
count++;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 4;
b = BitmapFactory.decodeFile(picturePath, options);
iv.setImageBitmap(Bitmap.createScaledBitmap(b, 480, 460, false));
main.addView(iv, count, lparams);
}
}
}
答案 0 :(得分:0)
看起来您正在加载图像,然后去拍摄图像。
您需要对路径进行处理,然后重新加载图像。除非您要将图像复制到您创建的文件(图库)中。在我看来这是不好的,因为空间会耗尽。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}