android 相机调用问题
三星系统4.0以上, 调用系统相机屏幕会强制切换为横屏 拍照完会切换为竖屏 点击保存回Activity 程序会直接崩溃 :
我理解的是
android系统在手机切换横竖屏的时候会把当前的activity销毁掉 然后又重新执行Oncreat方法 activity中设置得静态变量,所有的类变量居然都变成了空,而且在返回activity时重新执行oncreate(),由于变量都被回收,所以造成了空指针,目前解决方法也仅仅是在本activity中得变量在onSaveInstanceState()存储
开始贴代码
布局文件1:
android:configChanges="screenSize|keyboardHidden|orientation"
android:screenOrientation="portrait" >
布局文件2:
相机调用中间类
/**
* 照片生成的目录在 sd卡的/a/image/camera/.. .jpg
*
* @author baozi
*
*/
public class UseCameraActivity extends Activity {
private String mImageFilePath;
public static final String IMAGEFILEPATH = "ImageFilePath";
public final static String IMAGE_PATH = "image_path";
static Activity mContext;
public final static int GET_IMAGE_REQ = 5000;
private static Context applicationContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//判断 activity被销毁后 有没有数据被保存下来
if (savedInstanceState != null) {
mImageFilePath = savedInstanceState.getString(IMAGEFILEPATH);
Log.i("123---savedInstanceState", mImageFilePath);
File mFile = new File(IMAGEFILEPATH);
if (mFile.exists()) {
Intent rsl = new Intent();
rsl.putExtra(IMAGE_PATH, mImageFilePath);
setResult(Activity.RESULT_OK, rsl);
Log.i("123---savedInstanceState", "图片拍摄成功");
finish();
} else {
Log.i("123---savedInstanceState", "图片拍摄失败");
}
}
mContext = this;
applicationContext = getApplicationContext();
if (savedInstanceState == null) {
initialUI();
}
}
public void initialUI() {
//根据时间生成 后缀为 .jpg 的图片
long ts = System.currentTimeMillis();
mImageFilePath = getCameraPath() + ts + ".jpg";
File out = new File(mImageFilePath);
showCamera(out);
}
private void showCamera(File out) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); // set
startActivityForResult(intent, GET_IMAGE_REQ);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (GET_IMAGE_REQ == requestCode && resultCode == Activity.RESULT_OK) {
Intent rsl = new Intent();
rsl.putExtra(IMAGE_PATH, mImageFilePath);
mContext.setResult(Activity.RESULT_OK, rsl);
mContext.finish();
} else {
mContext.finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("ImageFilePath", mImageFilePath + "");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public static String getCameraPath() {
String filePath = getImageRootPath() + "/camera/";
File file = new File(filePath);
if (!file.isDirectory()) {
file.mkdirs();
}
file = null;
return filePath;
}
public static String getImageRootPath() {
String filePath = getAppRootPath() + "/image";
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
file = null;
return filePath;
}
public static String getAppRootPath() {
String filePath = "/a";
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
filePath = Environment.getExternalStorageDirectory() + filePath;
} else {
filePath = applicationContext.getCacheDir() + filePath;
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
file = null;
File nomedia = new File(filePath + "/.nomedia");
if (!nomedia.exists())
try {
nomedia.createNewFile();
} catch (IOException e) {
}
return filePath;
}
/*public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK&& event.getAction() == KeyEvent.ACTION_DOWN){
Intent inte =new Intent(UseCameraActivity.this, MainActivity.class);
startActivity(inte);
return true;
}
return super.onKeyDown(keyCode, event);
}*/
}
---------
然后是调用相机显示照片的activity
/**
* 启动界面
*
* 照片生成的目录在 sd卡的/a/image/camera/.. .jpg
*
* @author baozi
*
*/
public class MainActivity extends Activity implements OnClickListener{
private Bitmap bitmaps;
protected static final int REQCAMERA = 11;
private static final String TAG = "MainActivity";
private View button1;
private ImageView photo_iv;
private ContentResolver mContentResolver;
final int IMAGE_MAX_SIZE = 1024;
private ImageView lifttob,righttob,rightbom,liftbom;
private int volue =0;
protected static final int ADD_ID_CARD_FRONT = 101;
protected static final int ADD_ID_CARD_SIDE = 102;
protected static final int ADD_BANK_FRONT = 103;
protected static final int ADD_BANK_SIDE = 104;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentResolver = getContentResolver();
//判断 activity被销毁后 有没有数据被保存下来
if (savedInstanceState != null) {
volue =savedInstanceState.getInt("volue");
Log.i("123---savedInstanceState", "图片拍摄成功");
finish();
} else {
Log.i("123---savedInstanceState", "图片拍摄失败");
}
init();
}
private void init(){
lifttob = (ImageView) findViewById(R.id.btn_real_name_authentication_add_id_card_front);
righttob = (ImageView) findViewById(R.id.btn_real_name_authentication_add_id_card_side);
liftbom = (ImageView) findViewById(R.id.btn_bank_front);
rightbom = (ImageView) findViewById(R.id.btn_bank_side);
lifttob.setOnClickListener(this);
righttob.setOnClickListener(this);
rightbom.setOnClickListener(this);
liftbom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//按钮启动相机 volue赋值
case R.id.btn_real_name_authentication_add_id_card_front:
volue=1;
Intent intent = new Intent(MainActivity.this,
UseCameraActivity.class);
startActivityForResult(intent, ADD_ID_CARD_FRONT);
break;
case R.id.btn_real_name_authentication_add_id_card_side:
volue =2;
Intent intent1 = new Intent(MainActivity.this,
UseCameraActivity.class);
startActivityForResult(intent1, ADD_ID_CARD_SIDE);
break;
case R.id.btn_bank_front:
volue =3;
Intent intent2 = new Intent(MainActivity.this,
UseCameraActivity.class);
startActivityForResult(intent2, ADD_BANK_FRONT);
break;
case R.id.btn_bank_side:
volue =4;
Intent intent3 = new Intent(MainActivity.this,
UseCameraActivity.class);
startActivityForResult(intent3, ADD_BANK_SIDE);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ADD_ID_CARD_FRONT:
if(volue==1){
String path = data.getStringExtra(UseCameraActivity.IMAGE_PATH);
Log.i("123", path);
// 根据照片的位置获取图片
Bitmap bitmap = getBitmap(path);
Drawable dra = new BitmapDrawable(bitmap);
lifttob.setImageBitmap(bitmap);
}
break;
case ADD_ID_CARD_SIDE:
if(volue==2){
String path1 = data.getStringExtra(UseCameraActivity.IMAGE_PATH);
Bitmap bitmap1 = getBitmap(path1);
righttob.setImageBitmap(bitmap1);
}
case ADD_BANK_FRONT:
if(volue==3){
String path2 = data.getStringExtra(UseCameraActivity.IMAGE_PATH);
Bitmap bitmap2 = getBitmap(path2);
liftbom.setImageBitmap(bitmap2);
}
case ADD_BANK_SIDE:
if(volue==4){
String path3 = data.getStringExtra(UseCameraActivity.IMAGE_PATH);
Bitmap bitmap3 = getBitmap(path3);
rightbom.setImageBitmap(bitmap3);
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
/* if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else*/ if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
private Uri getImageUri(String path) {
return Uri.fromFile(new File(path));
}
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
//根据图片尺寸设置屏幕显示大小 o2.inSampleSize = scale; 也可以设置固定值 比如10
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(IMAGE_MAX_SIZE
/ (double) Math.max(o.outHeight, o.outWidth))
/ Math.log(0.5)));
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
in = mContentResolver.openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(in, null, o2);
in.close();
return b;
} catch (FileNotFoundException e) {
Log.e(TAG, "file " + path + " not found");
} catch (IOException e) {
Log.e(TAG, "file " + path + " not found");
}
return null;
}
//存入调用相机设置的volue值 为了防止图片设置重复
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("volue", volue );
}
}
demo下载地址: http://download.csdn.net/detail/qq_18413391/9557122