微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 FileOutputStream

如何解决在 FileOutputStream

下面是我的onActivityResult()

的全部代码
    @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);


    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
        ExifInterface exif = null;

        try {
            exif = new ExifInterface(imageFilePath);
        } catch (IOException e) {
            e.printstacktrace();
        }

        int exifOrientation;
        int exifDegree;

        if (exif != null) {
            exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_norMAL);
            exifDegree = exifOrientationToDegress(exifOrientation);
        }
        else {
            exifDegree = 0;
        }
        String result = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HHmmss",Locale.getDefault() );
        Date             curDate   = new Date(System.currentTimeMillis());
        String           filename  = formatter.format(curDate);

        String           strFolderName = Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + File.separator + "HENDRIK" + File.separator;

        File file = new File(strFolderName);
        if( !file.exists() )
            file.mkdirs();

        File f = new File(strFolderName + "/" + filename + ".png");
        result = f.getPath();

        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printstacktrace();
            result = "Save Error fOut";
        }


        rotate(bitmap,exifDegree).compress(Bitmap.CompressFormat.PNG,70,fOut);

        try {
            fOut.flush();
        } catch (IOException e) {
            e.printstacktrace();
        }

        try {
            fOut.close();
         
            mMediaScanner.mediaScanning(strFolderName + "/" + filename + ".png");
        } catch (IOException e) {
            e.printstacktrace();
            result = "File close Error";
        }

     

        ((ImageView) findViewById(R.id.iv_result)).setimageBitmap(rotate(bitmap,exifDegree));


    }

}

我想我有NullpointerException 错误

 FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printstacktrace();
            result = "Save Error fOut";
        }


        rotate(bitmap,fOut);

        try {
            fOut.flush();
        } catch (IOException e) {
            e.printstacktrace();
        }

似乎 fOut 为空,这就是错误发生的原因,但我不明白为什么 fOut 为空

下面是logcat 控制台

2021-07-13 18:55:35.908 8337-8337/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2021-07-13 18:55:35.909 8337-8337/? E/Zygote: accessInfo : 1
2021-07-13 18:55:35.924 8337-8337/? E/e.cameraexampl: UnkNown bits set in runtime_flags: 0x8000
2021-07-13 18:55:43.017 8337-8337/com.example.cameraexample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cameraexample,PID: 8337
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,request=672,result=-1,data=null} to activity {com.example.cameraexample/com.example.cameraexample.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5360)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5401)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8154)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.NullPointerException
        at android.graphics.Bitmap.compress(Bitmap.java:1685)
        at com.example.cameraexample.MainActivity.onActivityResult(MainActivity.java:144)
        at android.app.Activity.dispatchActivityResult(Activity.java:8300)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5353)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5401) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8154) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。