Android Studio:BitmapFactory.decodeFile错误

时间:2019-04-27 16:54:16

标签: android

我有一个使用相机捕获图像的活动。快照之后,它将调用以下onActivityResult。尝试解码文档时出现错误(行标记为“ ERROR HERE”)。原因的一些想法?谢谢

Java

//if you capture image, convert to bitmap, show on screen and call readTextFromImage
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_PICTURE_CAPTURE) {
                Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath()); // <--- ERROR HERE
                bitmap = Helper.rotateBitmap(bitmap, pictureFile.getAbsolutePath());
                image.setImageBitmap(bitmap);
            } else if (requestCode == REQUEST_GET_SINGLE_FILE) {
                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                } catch (Exception e) {
                    Log.d("Error Receipt: ", e.toString());
                }
                if (bitmap != null) {
                    image.setImageBitmap(bitmap);
                }
            }
            //readTextFromImage();
        }
    }

Logcat

04-27 18:25:19.276 16179-16179/com.example.ves.gennaio3 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ves.gennaio3, PID: 16179
    java.lang.RuntimeException: Unable to resume activity {com.example.ves.gennaio3/com.example.ves.gennaio3.ReceiptActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.ves.gennaio3/com.example.ves.gennaio3.ReceiptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
     Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.ves.gennaio3/com.example.ves.gennaio3.ReceiptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3107)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5443) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference
        at com.example.ves.gennaio3.ReceiptActivity.onActivityResult(ReceiptActivity.java:184)
        at android.app.Activity.dispatchActivityResult(Activity.java:6442)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3107) 
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5443) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

1 个答案:

答案 0 :(得分:0)

由于尚未实例化pictureFile,因此默认情况下为null。 试试

String pictureFile = data.getStringExtra("...","defaultPath");

然后可以使用BitmapFactory.decodeFile ... 如果您之前声明了pictureFile路径,请检查您的fileProvider。 问候, Csongi