如何使用onSaveInstanceState来记住用户上传的图像

时间:2017-05-30 19:57:17

标签: android

在我的应用中,我让用户从他/她的手机上传图像。当用户关闭应用程序然后再次打开它时,我希望图像仍然存在,但它不起作用。到目前为止,我的代码是:

private static int SELECT_PICTURE = 0;

ImageView imgImage;
Button btnUploadImage;

String filePath;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null) {

        filePath = savedInstanceState.getString("FILE_PATH");
        imgImage.setImageBitmap(BitmapFactory.decodeFile(filePath));
    }

    btnUploadImage = (Button) findViewById(R.id.btnUploadImage);
    imgImage = (ImageView) findViewById(R.id.imgImage);

    btnUploadImage.setOnClickListener(MainActivity.this);

}

@Override
public void onClick(View view) {

    selectImage();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bitmap bitmap = getPath(data.getData());

        imgImage.setImageBitmap(bitmap);
    }
}

private Bitmap getPath(Uri uri) {

    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    filePath = cursor.getString(column_index);
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);

    return bitmap;
}

private void selectImage() {

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

@Override
public void onSaveInstanceState(Bundle outState) {

   outState.putString("FILE_PATH", filePath);

    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

    savedInstanceState.getString("FILE_PATH");


    super.onRestoreInstanceState(savedInstanceState);
}

}

你能帮我解决一下代码吗?我认为问题在于onCreate方法,但我不知道如何更改代码。谢谢。

3 个答案:

答案 0 :(得分:0)

getString会返回String变量,您需要将其指定给您的字段

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

    filePath  = savedInstanceState.getString("FILE_PATH");


    super.onRestoreInstanceState(savedInstanceState);
}

但这仅适用于配置更改(例如旋转屏幕)。

如果您希望在应用程序被关闭时记住文件路径"即te用户退出并从最近的列表中滑动它。然后,您需要使用SharedPreferences将文件路径保存到内存:https://developer.android.com/training/basics/data-storage/shared-preferences.html

答案 1 :(得分:0)

在关闭和重新启动应用的情况下,您无法在var search = "generic phrase to search"; search = '\"' + search.split(' ').join('\" \"') + '\"'; db.collection.find({$text:{$search:search}}, function... 上使用。您可以在#use groupby, count As and get percentage, finally pivot month to columns. df.groupby(by=['Year','Month'])['Decision'].apply(lambda x: len(x[x=='A'])/float(len(x))).reset_index(level=1).pivot(columns='Month').fillna(0) Out[257]: Decision Month 1 2 3 Year 2003 1.0 0.0 0.0 2004 1.0 0.0 0.0 2005 0.0 0.0 1.0 中保存图片路径,然后检索它并在onCreate方法的onSaveInstanceState中加载图片

SharedPreferences

答案 2 :(得分:0)

可以在用户旋转屏幕时使用RestoreInstanceState,或者更改语言(强制Activity销毁并重新创建一次)。

如果用户关闭应用程序,那不起作用,因此您可以使用共享首选项,或将信息保存在其他地方。

这是您使用共享首选项的方法:

保存一些密钥:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.String("ImagePath", yourImagePathToSave);
editor.commit()

再来一次:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String imagePathSaved = sharedPref.getString("ImagePath", null); //null represents the default value.

Android Developer Docs