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

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

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

最近在作一发表的功能时,须要从系统相册中选择图片,最后有将此图片上传服务端。一般从系统相册中选择图片写法以下:ide

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中获取刚刚选取的照片:post

 1 @Override  2 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {  3     if (resultCode == RESULT_OK) {  4         if (requestCode == AppContext.GETIMAGE_BYSDCARD || requestCode == AppContext.GETIMAGE_BYCAMERA) {  5             if (requestCode == AppContext.GETIMAGE_BYSDCARD && null != data) {  6                 Uri selectedImage = data.getData();  7                 String[] filePathColumn = { MediaStore.Images.Media.DATA };  8                 Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);  9                 if (cursor != null) { 10                     if (cursor.moveToFirst()) { 11                         //int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
12                         int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]); 13                         photoPath = cursor.getString(columnIndex); 14  } 15  cursor.close(); 16  } 17  } 18  } 19  } 20 }

能够在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,即直接返回了所选取图片的地址,所以,须要针对性的作出以下处理:ui

 1 @Override  2 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {  3     if (resultCode == RESULT_OK) {  4         if (requestCode == AppContext.GETIMAGE_BYSDCARD || requestCode == AppContext.GETIMAGE_BYCAMERA) {  5             if (requestCode == AppContext.GETIMAGE_BYSDCARD && null != data) {  6                 Uri selectedImage = data.getData();  7                 String[] filePathColumn = { MediaStore.Images.Media.DATA };  8                 Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);  9                 if (cursor != null) { 10                     if (cursor.moveToFirst()) { 11                         //int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
12                         int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]); 13                         photoPath = cursor.getString(columnIndex); 14  } 15  cursor.close(); 16                 } else { 17                     if (selectedImage != null) { 18                         String tmpPath = selectedImage.getPath(); 19                         if (tmpPath != null && (tmpPath.endsWith(".jpg") || tmpPath.endsWith(".png") || tmpPath.endsWith(".gif"))) { 20                             photoPath = tmpPath; 21  } 22  } 23  } 24  } 25  } 26  } 27 }

 

其实对于选择系统相册的此类功能,我的感受最完整的作法应该相似于微信中的选择图片,因为Android兼容性问题的存在,致使目前没有彻底有效的统一方法完成照片的选择。spa

----------------------------指针

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

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

 1 public static int getPictureDegree(String path) {  2     int degree = 0;  3     try {  4         ExifInterface exifInterface = new ExifInterface(path);  5         int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  6         switch (orientation) {  7         case ExifInterface.ORIENTATION_ROTATE_90:  8             degree = 90;  9             break; 10         case ExifInterface.ORIENTATION_ROTATE_180: 11             degree = 180; 12             break; 13         case ExifInterface.ORIENTATION_ROTATE_270: 14             degree = 270; 15             break; 16  } 17     } catch (IOException e) { 18  e.printStackTrace(); 19  } 20     return degree; 21 }
 1 int degree = getPictureDegree(filePath);  2 return roateBitmap(thumbBitmap, degree);  3 
 4 public static Bitmap roateBitmap(Bitmap bitmap, int degree) {  5     if (degree == 0) {  6         return bitmap;  7  }  8     Matrix matrix = new Matrix();  9  matrix.postRotate(degree); 10     Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 11     return bmp; 12 }

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

 

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

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

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

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

1 downloadNotification.flags |= Notification.FLAG_ONGOING_EVENT;