Android系统兼容性问题(持续更新)

相信开发过一段Android的都被Android中的兼容性问题给折腾过,有时这确实很无奈,Android被不同的厂商改的七零八落的。本文主要总结下本人在实际的项目开发过程中所遇到的兼容性问题,以及最后的解决办法。本文将持续更新。

1. 选择系统相册时HTC 7出现的系统崩溃(空指针异常) 系统版本 2.3.7

最近在做一发表的功能时,需要从系统相册中选择图片,最后有将此图片上传服务端。通常从系统相册中选择图片写法如下:

albumButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, AppContext.GETIMAGE_BYSDCARD);
}
});

然后在onActivityResult中获取刚刚选取的照片:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
if (requestCode == AppContext.GETIMAGE_BYSDCARD || requestCode == AppContext.GETIMAGE_BYCAMERA) {
if (requestCode == AppContext.GETIMAGE_BYSDCARD && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
//int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
photoPath = cursor.getString(columnIndex);
}
cursor.close();
}
}
}
}
}

可以在HTC7  2.3.7 上发现无法获取图片,如果上述代码中没有做cursor != null 则系统崩溃,最后定为出原因在于Uri selectedImage = data.getData();这行代码上,在其他手机上,此处返回格式为content://media/external/images/media/244709,因此自然是通过接下来的Content Privider方式获取到图片实际地址。而在HTC此手机上,返回的结果却为:/storage/sdcard0/DCIM/Camera/IMG_20140608_162447.jpg,即直接返回了所选取图片的地址,因此,需要针对性的做出如下处理:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
if (requestCode == AppContext.GETIMAGE_BYSDCARD || requestCode == AppContext.GETIMAGE_BYCAMERA) {
if (requestCode == AppContext.GETIMAGE_BYSDCARD && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
//int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
photoPath = cursor.getString(columnIndex);
}
cursor.close();
} else {
if (selectedImage != null) {
String tmpPath = selectedImage.getPath();
if (tmpPath != null && (tmpPath.endsWith(".jpg") || tmpPath.endsWith(".png") || tmpPath.endsWith(".gif"))) {
photoPath = tmpPath;
}
}
}
}
}
}
}

其实对于选择系统相册的此类功能,个人感觉最完整的做法应该类似于微信中的选择图片,由于Android兼容性问题的存在,导致目前没有完全有效的统一方法完成照片的选择。

----------------------------

2.三星手机调用手机拍照后出现横竖屏切换的问题

其实这也是一个典型的问题了,当初也是在网上直接查到的解决方案,貌似是三星手机的通病。解决方案如下:

 public static int getPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
 int degree = getPictureDegree(filePath);
return roateBitmap(thumbBitmap, degree); public static Bitmap roateBitmap(Bitmap bitmap, int degree) {
if (degree == 0) {
return bitmap;
}
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bmp;
}

主要思路其实就是通过图片Exif获取到其旋转角度,然后再相应旋转过来。

3.Android 2.3及以下系统Notification出现IllegalArgumentException: contentIntent required

对于2.3及以下系统时,在创建Notification时需要设置其对应的contentIntent,可以直接通过mNotification.contentIntent = xxx直接设置或mNotification.setLatestEventInfo(...)间接设置。有时候,contentIntent对应于用户点击此通知时所触发的响应,有时候,这种响应是不必要的,如当通知栏中显示下载进度条时,在进度条尚未达到100%之前,用户点击通知实际上是不希望有什么操作的。但2.3及以下系统不设置contentIntent又会如此报错。解决方式如下:

 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
Intent intent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
mNotification.contentIntent = contentIntent;
}

此时,当用户点击通知时,会发现此通知消失,虽然不能进行后续动作上的特定跳转。显然,这也是不符合实际需要的。可以通过如下方式解决。

 downloadNotification.flags |= Notification.FLAG_ONGOING_EVENT;
上一篇:14 Using Indexes and Clusters


下一篇:页面设计--CheckBoxList