抓取圖像時,我有下面的代碼,其用於抓取的圖像(無論是從照相機或畫廊),然後在一個ImageView的顯示它:空指針異常從廊
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK)
{
if(requestCode == 1888)
{
final boolean isCamera;
if(data == null)
{
isCamera = true;
}
else
{
final String action = data.getAction();
if(action == null)
{
isCamera = false;
}
else
{
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUriTemp;
Uri selectedImageUri;
if(isCamera)
{
selectedImageUriTemp = outputFileUri;
Bitmap image = decodeFile(new File(selectedImageUriTemp.getPath()));
selectedImageUri = getImageUri(image);
}
else
{
selectedImageUriTemp = data == null ? null : data.getData();
Bitmap image = decodeFile(new File(selectedImageUriTemp.getPath()));
selectedImageUri = getImageUri(image);
}
Log.i("TAG", "IMAGEURI: " + selectedImageUri);
pictureThumb.setImageURI(selectedImageUri);
setRealPath(selectedImageUri);
}
}
}
private Uri getImageUri(Bitmap image){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(getApplicationContext().getContentResolver(), image, "Title", null);
return Uri.parse(path);
}
代碼工作,如果我嘗試使用相機抓取圖像,但是當我嘗試從圖庫抓取圖像時,它會返回空指針異常。
任何人都可以向我建議正確的方向嗎?由於
這裏是完整的日誌:
02-12 10:34:56.249: E/AndroidRuntime(14607): FATAL EXCEPTION: main
02-12 10:34:56.249: E/AndroidRuntime(14607): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { dat=content://media/external/images/media/29 }} to activity {com.gelliesmedia.thumbqoo/com.gelliesmedia.thumbqoo.PictureBooth}: java.lang.NullPointerException: uriString
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.app.ActivityThread.deliverResults(ActivityThread.java:3216)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3259)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.app.ActivityThread.access$1100(ActivityThread.java:138)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.os.Looper.loop(Looper.java:137)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.app.ActivityThread.main(ActivityThread.java:4954)
02-12 10:34:56.249: E/AndroidRuntime(14607): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 10:34:56.249: E/AndroidRuntime(14607): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 10:34:56.249: E/AndroidRuntime(14607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
02-12 10:34:56.249: E/AndroidRuntime(14607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
02-12 10:34:56.249: E/AndroidRuntime(14607): at dalvik.system.NativeStart.main(Native Method)
02-12 10:34:56.249: E/AndroidRuntime(14607): Caused by: java.lang.NullPointerException: uriString
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.net.Uri$StringUri.<init>(Uri.java:464)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.net.Uri$StringUri.<init>(Uri.java:454)
02-12 10:34:56.249: E/AndroidRuntime(14607): at android.net.Uri.parse(Uri.java:426)
02-12 10:34:56.249: E/AndroidRuntime(14607): at com.gelliesmedia.thumbqoo.PictureBooth.getImageUri(PictureBooth.java:224)
02-12 10:34:56.249: E/AndroidRuntime(14607): at com.gelliesmedia.thumbqoo.PictureBooth.onActivityResult(PictureBooth.java:206)
這爲我工作從mediastore查詢。謝謝 – Sotti