【问题标题】:Saving activity state with photos loaded from phone?保存从手机加载的照片的活动状态?
【发布时间】:2013-12-05 14:06:43
【问题描述】:

我有一个活动,可以从您的图库中拍摄照片并将它们放在屏幕上,但是当我退出应用程序并返回时,它们不会保存在那里。当我退出并返回时,如何将它们保存在活动中? 一直在尝试 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);
    }
}

}

【问题讨论】:

    标签: android bitmap gallery savestate


    【解决方案1】:

    看起来您正在加载图像,然后试图保存图像。

    您需要保存路径,然后重新加载图像。除非您要将图像复制到您创作的文件(图库)中。我认为这很糟糕,因为空间会用完。

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

    【讨论】:

    • 看看你在哪里保存路径 String picturePath = cursor.getString(columnIndex);保存该字符串而不是 toSave.putParcelable("bitmap", b); savestate 是 onSaveInstanceState 的缩写
    • 好吧,我正在尝试这样的事情:@Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putString("imagePath", picturePath); super.onSaveInstanceState(savedInstanceState); } and if (savedInstanceState != null) { // 从保存状态中恢复成员的值 picturePath = savedInstanceState.getString("imagePath");但它不起作用。我错过了什么?
    • 您是否在 on create AND onRestore 中调用了 if 语句?见developer.android.com/training/basics/activity-lifecycle/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2015-06-17
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多