我的错误只发生在某些手机上。例如,它发生在Android 2.3版本的手机中(在我的情况下是一个三星galaxy mini)。在我的设备中它完美运行(三星galaxy S2 Android v 4.0.4) 当我拍摄照片并尝试将其用于布局时,我在使用manageQuery的行中出现错误。这是我正在使用的代码:
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=(int) 0.9;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case ChooseImageInputDialog.ACTIVITY_SELECT_PHOTO:
if(resultCode == RESULT_OK){
Log.d("resultcode pea pick", "result ok");
Uri selectedImageUri=data.getData();
actualPath=getRealPathFromURI(selectedImageUri);
File file=new File(actualPath);
bmp=decodeFile(file); //this is new bitmap which you can use for your purpose
ByteArrayOutputStream os = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG, 65, os);
photoByteArray = os.toByteArray();
ivPhoto.setImageBitmap(bmp);
}else{
Log.d("resultcode pea pick", "result not ok");
}
break;
case ChooseImageInputDialog.ACTIVITY_CAMERA_PHOTO:
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
//bmp = (Bitmap)extras.get("data");
Uri selectedImageUri=data.getData();
actualPath=getRealPathFromURI(selectedImageUri);
File file=new File(actualPath);
bmp=decodeFile(file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG, 65, os);
photoByteArray = os.toByteArray();
ivPhoto.setImageBitmap(bmp);
actualPath = getRealPathFromURI(data.getData());
Log.d("take pic actual path", actualPath);
}
break;
}
错误如下:
01-19 19:16:57.389: E/AndroidRuntime(3024): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1111, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.play.playwithme/com.play.playwithme.PhotoEditingActivity}: java.lang.NullPointerException
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.os.Looper.loop(Looper.java:130)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-19 19:16:57.389: E/AndroidRuntime(3024): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 19:16:57.389: E/AndroidRuntime(3024): at java.lang.reflect.Method.invoke(Method.java:507)
01-19 19:16:57.389: E/AndroidRuntime(3024): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-19 19:16:57.389: E/AndroidRuntime(3024): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-19 19:16:57.389: E/AndroidRuntime(3024): at dalvik.system.NativeStart.main(Native Method)
01-19 19:16:57.389: E/AndroidRuntime(3024): Caused by: java.lang.NullPointerException
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.content.ContentResolver.query(ContentResolver.java:256)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.Activity.managedQuery(Activity.java:1550)
01-19 19:16:57.389: E/AndroidRuntime(3024): at com.play.playwithme.PhotoEditingActivity.getRealPathFromURI(PhotoEditingActivity.java:133)
01-19 19:16:57.389: E/AndroidRuntime(3024): at com.play.playwithme.PhotoEditingActivity.onActivityResult(PhotoEditingActivity.java:193)
01-19 19:16:57.389: E/AndroidRuntime(3024): at android.app.Activity.dispatchActivityResult(Activity.java:3908)