believe that the development of an android has been the compatibility of Android issues to toss over, and sometimes it is very helpless, Android by different manufacturers to change the pieces. This article mainly summarizes the actual project development process I encountered in the compatibility problems, as well as the final solution. This article will be updated continuously.
1. System crash (null pointer exception) in HTC 7 when selecting a System album System version 2.3.7
Recently in the function of publishing, you need to select a picture from the system album, and finally upload this image to the server. You usually choose a picture from the system album as follows:
Albumbutton.setonclicklistener (new View.onclicklistener () { @Override public void OnClick (View v) { new Intent (Intent.action_pick, Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Startactivityforresult (Intent, Appcontext.getimage_bysdcard); });
Then get the photo you just picked in Onactivityresult:
1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, 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) {6Uri SelectedImage =Data.getdata ();7string[] Filepathcolumn ={MediaStore.Images.Media.DATA};8cursor cursor = getcontentresolver (). Query (SelectedImage, Filepathcolumn,NULL,NULL,NULL);9 if(Cursor! =NULL) {Ten if(Cursor.movetofirst ()) { One //int columnindex = Cursor.getcolumnindex (filepathcolumn[0]); A intColumnIndex = Cursor.getcolumnindexorthrow (filepathcolumn[0]); -Photopath =cursor.getstring (columnindex); - } the cursor.close (); - } - } - } + } -}
Can be found on the HTC7 2.3.7 cannot get the picture, if the above code does not do cursor! = NULL The system crashes, and finally the reason is that the URI selectedimage = Data.getdata (); This line of code, on other phones, The return format here is content://media/external/images/media/244709, so it is natural to get the actual address of the picture through the next content Privider method. On HTC This phone, the return result is:/storage/sdcard0/dcim/camera/img_20140608_162447.jpg, that is, directly return the address of the selected picture, therefore, you need to make the following processing:
1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, 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) {6Uri SelectedImage =Data.getdata ();7string[] Filepathcolumn ={MediaStore.Images.Media.DATA};8cursor cursor = getcontentresolver (). Query (SelectedImage, Filepathcolumn,NULL,NULL,NULL);9 if(Cursor! =NULL) {Ten if(Cursor.movetofirst ()) { One //int columnindex = Cursor.getcolumnindex (filepathcolumn[0]); A intColumnIndex = Cursor.getcolumnindexorthrow (filepathcolumn[0]); -Photopath =cursor.getstring (columnindex); - } the cursor.close (); -}Else { - if(SelectedImage! =NULL) { -String Tmppath =Selectedimage.getpath (); + if(Tmppath! =NULL&& (Tmppath.endswith (". jpg") | | tmppath.endswith (". png") | | tmppath.endswith (". gif")))) { -Photopath =Tmppath; + } A } at } - } - } - } -}
----------------------------
One day to tidy up a little update ...