I create a android application.Select image from gallery using a button and retrieve in the ImageView. The image fetching is successful. Now I want to saved the state of the selected Image.I try to fix.It makes crash application.When I change the Horizontal orientation the app is crashed.Please help me to solve the issue.
My Code :
public class MainActivity extends ActionBarActivity {
ImageView imgBackground;
Button loadImgBtn;
String imgDecodableString;
Drawable drawable;
private static int RESULT_LOAD_IMG = 1;
private static final String IMAGE_DATA = "image_resource";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadImgBtn = (Button)findViewById(R.id.btnSelectImage);
imgBackground = (ImageView)findViewById(R.id.myImg);
loadImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Create intent to open Image Application like Gallery */
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
/* start the Intent */
startActivityForResult(galleryIntent,RESULT_LOAD_IMG);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
/* Get the Image from Data */
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
/* Get the Cursor */
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn,null,null,null);
/* Move the first row */
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
/* Rendering the Image */
drawable = new BitmapDrawable(imgDecodableString);
imgBackground.setBackgroundDrawable(drawable);
}
} catch (Exception e) {
message(getBaseContext()," Error : " + e.getMessage(),Toast.LENGTH_SHORT);
}
}
public void message(Context ctx,String msg,int duration) {
Toast.makeText(ctx,msg,duration).show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(IMAGE_DATA, (android.os.Parcelable) drawable);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
drawable = (Drawable) savedInstanceState.getParcelable(IMAGE_DATA);
}
}
I have not tested this, so I don't know if it is working. Hope it is.
But you should be aware of this
...it might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with the onSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a stateful Object when your activity is restarted due to a configuration change.
public class MainActivity extends ActionBarActivity {
ImageView imgBackground;
Button loadImgBtn;
String imgDecodableString;
BitmapDrawable drawable;
private static int RESULT_LOAD_IMG = 1;
private static final String IMAGE_DATA = "image_resource";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadImgBtn = (Button)findViewById(R.id.btnSelectImage);
imgBackground = (ImageView)findViewById(R.id.myImg);
loadImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Create intent to open Image Application like Gallery */
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
/* start the Intent */
startActivityForResult(galleryIntent,RESULT_LOAD_IMG);
}
});
if(savedInstanceState != null) {
Bitmap tmp = savedInstanceState.getParcelable(IMAGE_DATA);
if(tmp != null) {
drawable = new BitmapDrawable(getResources(), tmp);
imgBackground.setImageDrawable(drawable);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
/* Get the Image from Data */
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
/* Get the Cursor */
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn,null,null,null);
/* Move the first row */
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
/* Rendering the Image */
drawable = new BitmapDrawable(imgDecodableString);
imgBackground.setBackgroundDrawable(drawable);
}
} catch (Exception e) {
message(getBaseContext()," Error : " + e.getMessage(), Toast.LENGTH_SHORT);
}
}
public void message(Context ctx,String msg,int duration) {
Toast.makeText(ctx,msg,duration).show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(drawable != null && drawable.getBitmap() != null) {
outState.putParcelable(IMAGE_DATA, drawable.getBitmap());
}
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With