I am trying save an ImageButton’s path in savedInstanceState in order not lose image for rotating the phone. But only one time it’s working after that the path value i equal null (path==null)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentLayout(R.layout.contact_information_wga800);
if (savedInstanceState != null) {
String profilefirstname = savedInstanceState.getString("firstname");
firstname.setText(profilefirstname);
String profilepath=savedInstanceState.getString("path");
if(profilepath !=null){
imagebutton.setImageBitmap( BitmapFactory.decodeFile(profilepath));
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putString("firstname", firstname.getText().toString());
savedInstanceState.putString("path", media_path);
super.onSaveInstanceState(savedInstanceState);
}
This is the part when I set in imagebutton bitmap using camera or android gallery.
switch(requestCode) {
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
final Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
if( bmp != null ) {
final File f = this.getFileStreamPath("ping_media.jpg");
if (f.exists()) {
f.delete();
}
try {
final FileOutputStream out = this.openFileOutput("ping_media.jpg",MODE_PRIVATE);
bmp.compress(CompressFormat.JPEG, 12, out);
out.close();
media_path = f.getAbsolutePath();
if(dm.densityDpi>=200) {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 72, 72), 72));
} else if(dm.densityDpi>130 && dm.densityDpi<200) {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 48, 48), 48));
} else {
contactimg.setImageBitmap(getRoundedCornerBitmap(getResizedBitmap(bmp, 32, 32), 32));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
break ;
case REQUEST_SELECT_PHOTO:
if( resultCode != 0 ) {
final Cursor c = managedQuery(data.getData(),null,null,null,null);
if (c.moveToFirst()) {
media_path = c.getString(1);
bmp = BitmapFactory.decodeFile(media_path);
contactimg.setImageBitmap(getRoundedCornerBitmap( getResizedBitmap(bmp, 48, 48), 48));
}
}
break;
}
should I do here so anytime rotating the phone I didn’t lose imagebutton’s bitmap.
It seems like you are using the member variable media_path to store the path. You need to update this member when reading the path from the Bundle again in onCreate. Otherwise it is still null when you try to save it in onSaveInstanceState (except when your switch block has been run in the mean time).