import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv;
Bitmap bTemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cameraClick = (Button) findViewById(R.id.click);
iv = (ImageView) findViewById(R.id.imageView1);
final Bitmap data = (Bitmap) getLastNonConfigurationInstance();
if (data == null) {
iv.setImageBitmap(bTemp);
}
cameraClick.setOnClickListener(myhandler);
}
OnClickListener myhandler = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bm);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
@Deprecated
public Object onRetainNonConfigurationInstance() {
bTemp = iv.getDrawingCache();
return bTemp;
}
}
I'm using a imageview to store the image which is captured using the Camera Intent but when the screen rotates the image is lost. I tried using onRetainNonConfigurationInstance() but its not working
And i don't want to write the image to afile.
the recommended way is to use setRetainInstance(true) in the fragment to retain images. Runtime configuration changes, such as a screen orientation change, cause Android to destroy and restart the running activity with the new configuration (For more information about this behavior, see Handling Runtime Changes).
Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.
If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.
When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.
You can avoid recreating the activity by setting the orientation and screensize flag in your Manifest file
android:configChanges="keyboard|orientation|screenSize"
If needed you can implement onConfigurationChanged()
which will be called when the orientation changes. More information is in http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
this may help you...
@Override
protected void onSaveInstanceState(Bundle outState) {
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
outState.putParcelable("image", bitmap);
super.onSaveInstanceState(outState);
}
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null) {
Bitmap bitmap = savedInstanceState.getParcelable("image");
imageView.setImageBitmap(bitmap);
}
}
The reason why your image disappears when screen rotates is that the activity is destroyed and recreated. During such process, the selected image is not retained as you can read here
Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).
If you want to retain the selected image you should not use Instance State as you can read here.
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 Fragment when your activity is restarted due to a configuration change. This fragment can contain references to stateful objects that you want to retain.
I implemented this solution and you can find the code in my answer to this other question
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