0

该方法适用于内置相机拍摄的图像,但对于来自外部来源(网络、物理相机等)的图像,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());
    }
}

谢谢您的帮助!

4

1 回答 1

3

问题是图像来自 Picasa,因此它们没有存储在 SD 上,从而创建了 NullPointerException。你可以设置

intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)

在启动活动以将画廊中的项目仅限于 SD 卡上的项目之前。如果我找到一个,我会发布一个更好的解决方案。

于 2013-07-29T23:13:50.287 回答