このメソッドは、内蔵カメラで撮影された画像に対しては正常に機能しますが、外部ソース (Web、物理カメラなど) からの画像に対しては 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());
}
}
ご協力ありがとうございました!