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;
}
}
我正在使用 imageview 来存储使用 Camera Intent 捕获的图像,但是当屏幕旋转时,图像会丢失。我尝试使用 onRetainNonConfigurationInstance() 但它不起作用
而且我不想将图像写入文件。