该方法适用于内置摄像头拍摄的图像,但对于来自外部源(网络,物理相机等)的图像,BitmapFactory.decodeStream(imageStream)返回null。有谁知道可能导致错误的原因?
这是堆栈跟踪:
07-29 17:25:48.105: E/AndroidRuntime(32296): FATAL EXCEPTION: main
07-29 17:25:48.105: E/AndroidRuntime(32296): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.gallery3d.provider/picasa/item/5716149895129356946 flg=0x1 }} to activity {com.example.news_app/com.example.news_app.android.ui.SettingsActivity}: java.lang.NullPointerException
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.os.Looper.loop(Looper.java:137)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-29 17:25:48.105: E/AndroidRuntime(32296): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 17:25:48.105: E/AndroidRuntime(32296): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 17:25:48.105: E/AndroidRuntime(32296): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-29 17:25:48.105: E/AndroidRuntime(32296): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-29 17:25:48.105: E/AndroidRuntime(32296): at dalvik.system.NativeStart.main(Native Method)
07-29 17:25:48.105: E/AndroidRuntime(32296): Caused by: java.lang.NullPointerException
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:474)
07-29 17:25:48.105: E/AndroidRuntime(32296): at com.example.news_app.android.ui.SettingsActivity.onActivityResult(SettingsActivity.java:84)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
07-29 17:25:48.105: E/AndroidRuntime(32296): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
07-29 17:25:48.105: E/AndroidRuntime(32296): ... 11 more
以下是我正在使用的方法:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
Log.d("onActivityResult", "FileNotFoundException");
e.printStackTrace();
}
Bitmap unscaledImage = BitmapFactory.decodeStream(imageStream);
image = Bitmap.createScaledBitmap(unscaledImage, IMAGE_SIZE, IMAGE_SIZE, false);
Log.d("Unscaled image byte count", "Bytes: " + unscaledImage.getByteCount());
Log.d("Scaled image byte count", "Bytes: " + image.getByteCount());
}
}
感谢您的帮助!
答案 0 :(得分:3)
问题是图片来自Picasa,所以它们没有存储在SD上,因此会产生NullPointerException。你可以设置
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
在启动活动之前,仅将图库中的项目限制为SD卡上的项目。如果找到一个,我会发布一个更好的解决方案。