Android相机调用后直接闪退的问题

现象:

1、在性能比较好的Android手机,比如nexus 6,小米4等手机上拍完照,并截图后,均能够正常更换头像。

2、在genymotion的模拟器上的sumsung galaxy s3以及nexus 6上拍照截图均正常。

3、小米2/2s的Android4.4.4上时而正常,时而发生闪退现象。

4、试用过程中有其他同事反映拍照功能异常。

归类:

归类为手机兼容性问题,要么是软件系统兼容导致,要么是硬件配置过低导致。

具体原因分析:

1、图片太大。手机内存不够。

2、本身参数设置不正确。

3、activity被杀掉。

问题排查:

1、在拍照过程中涉及到文件保存,以及Bitmap decode。及时释放打开的文件流和bitmap对象。节省内存。

2、图片crop时,将分辨率适当设小,防止图片过大。另外截图保存在uri中。图片代码如下:

/**

* 收缩图片

*

*@paramuri,数据源

*/

public voidstartPhotoZoom(Uri uri) {

Intent intent =newIntent("com.android.camera.action.CROP");//调用Android系统自带的一个图片剪裁页面,

intent.setDataAndType(uri,IMAGE_UNSPECIFIED);//INTENT需要打开的文件uri及文件类型

intent.putExtra("crop","true");//进行修剪

// aspectX aspectY 是宽高的比例

intent.putExtra("aspectX",1);

intent.putExtra("aspectY",1);

// outputX outputY 是裁剪图片宽高,设置为200,防止图片过大intent.putExtra("outputX",200);

intent.putExtra("outputY",200);

//        intent.putExtra("return-data", true);//intent can not pass data over 1Mbintent.putExtra("return-data",false);//intent中不返回具体二进制,防止内存占用

//将截图保存在uri中File tempFile =newFile(Environment

.getExternalStorageDirectory(),"tempCrop.jpg");

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//设置图片格式

this.cropTempFile= tempFile.getPath();

Log.d(TAG,"cropTemp file:"+cropTempFile);

this.target.startActivityForResult(intent,PHOTO_RESULT);

}

3、crop返回的数据保存时,进行压缩。代码如下:

// 处理结果

if(requestCode ==PHOTO_RESULT) {

Bundle extras = data.getExtras();

if(extras !=null) {

//                Bitmap photo = extras.getParcelable("data"); //不在intent中直接传递数据头像

BitmapFactory.Options options =newBitmapFactory.Options();

options.inSampleSize=2;

Bitmap photo = BitmapFactory.decodeFile(this.cropTempFile,options);

File fileDir = Environment.getExternalStorageDirectory();

FileOutputStream out =null;

try{

File storeFile =newFile(fileDir,Constant.HEAD_PIC_NAME);

out =newFileOutputStream(storeFile);

photo.compress(Bitmap.CompressFormat.JPEG,75, out);// (0-100)压缩文件

out.flush();

//save portrait

this.localPortraitPath= storeFile.toURI().toString();

savePortrait(ServerEnvs.getAppBaseUrl() +"/gateway/saveStoneIcon", storeFile);

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}finally{

if(out !=null){

try{

out.close();

photo.recycle();//回收头像

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

}

4、防止Activity在手机全屏,以及旋转时被回收。

<android:name=".module.myself.activity.HeadChangeActivity"

android:configChanges="orientation|screenSize|keyboardHidden|keyboard"

android:screenOrientation="portrait"

android:theme="@style/translucent"/>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值