我有此错误:打开失败:ENOENT(无此文件或目录)

时间:2020-08-17 10:56:24

标签: java android image path bitmap

我正在打开失败错误。我要尝试从电子邮件发送附件,但电子邮件失败,因为我失败了,图像路径错误。已经更改了很多次图像路径,并且还添加了未使用权限的清单文件。错误是什么,请帮助我(打开失败:ENOENT(没有此类文件或目录)。

public class CameraActivity extends AppCompatActivity {
private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
TextureView textureView;
Button imgCaptureFront, flashbtn;
FlashMode flash;
private int REQUEST_CODE_PERMISSIONS = 101;
private CameraX.LensFacing lensfacing = CameraX.LensFacing.BACK;
private ImageCapture imgCap;
// private String filePath,uniqueId,filetostring;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);


    textureView = findViewById(R.id.view_finder);
    imgCaptureFront = findViewById(R.id.imgCaptureFront);
    flashbtn = findViewById(R.id.flashbtn);
    Bundle cameraBundle = getIntent().getExtras();
    // filePath = (String) cameraBundle.get("file");
    imgCaptureFront.setText("FRONT");

    // uniqueId = (String) cameraBundle.get("uniqueId");
    if (allPermissionsGranted()) {
        startCamera(); //start camera if permission has been granted by user
    } else {
        ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
    }
    imgCaptureFront.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            frontback();
        }
    });
    imgCaptureFront = findViewById(R.id.imgCaptureFront);
    flashbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            flashmode();
        }
    });
}

private void flashmode() {
    flash = imgCap.getFlashMode();
    if (FlashMode.ON == flash) {
        imgCap.setFlashMode(FlashMode.OFF);
        flashbtn.setText("FLASH OFF");
    } else {
        imgCap.setFlashMode(FlashMode.ON);
        flashbtn.setText("FLASH ON");
    }
}

private void frontback() {
    if (CameraX.LensFacing.FRONT == lensfacing) {
        lensfacing = CameraX.LensFacing.BACK;
        imgCaptureFront.setText("FRONT");
    } else {
        lensfacing = CameraX.LensFacing.FRONT;
        imgCaptureFront.setText("BACK");
    }
    startCamera();
}

private void startCamera() {

    CameraX.unbindAll();

    Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight());
    Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen


    PreviewConfig pConfig = new PreviewConfig.Builder()
            .setTargetAspectRatio(aspectRatio)
            .setTargetResolution(screen)
            .setLensFacing(lensfacing)
            .build();
    Preview preview = new Preview(pConfig);

    preview.setOnPreviewOutputUpdateListener(
            new Preview.OnPreviewOutputUpdateListener() {
                @Override
                public void onUpdated(Preview.PreviewOutput output) {
                    ViewGroup parent = (ViewGroup) textureView.getParent();
                    parent.removeView(textureView);
                    parent.addView(textureView, 0);

                    textureView.setSurfaceTexture(output.getSurfaceTexture());
                    updateTransform();
                }
            });


    ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder()
            //.setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY)
            .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
            .setLensFacing(lensfacing)
            .setFlashMode(FlashMode.ON)
            .build();
    imgCap = new ImageCapture(imageCaptureConfig);

    findViewById(R.id.imgCapture).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String fileName = "text.txt";
            // File file = new File(CameraActivity.this.getFilesDir().getPath() + "/" + System.currentTimeMillis() + ".png");
            File file = new File(CameraActivity.this.getFilesDir().getPath() + File.separator + getPackageName());
            File destination = new File(CameraActivity.this.getExternalFilesDir(null) + "/" + getPackageName());

            imgCap.takePicture(destination, new ImageCapture.OnImageSavedListener() {
                @Override
                public void onImageSaved(@NonNull File file) {
                    DataConst dataConst = new DataConst();
                    String msg = "Pic captured at " + file.getAbsolutePath();
                    dataConst.imagepath = msg;
                    Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.putExtra("filePath", file.getAbsolutePath());
                    intent.putExtra("filePathABS", file.getAbsolutePath());


                    Log.e("filepathabs", ":" + file.getAbsolutePath());
                    // intent.putExtra("uniqueId", uniqueId);
                    boolean screenOn = intent.getBooleanExtra("screen_state", false);
                    if (!screenOn) {

                        MainActivity mainActivity = new MainActivity();
                        mainActivity.sendEmail();
                    } else {

                        MainActivity mainActivity = new MainActivity();
                        mainActivity.sendEmail();
                    }
                    setResult(RESULT_OK, intent);

                    finish();
                }

                @Override
                public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
                    String msg = "Pic capture failed : " + message;
                    Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                    if (cause != null) {
                        cause.printStackTrace();
                    }
                }
            });
        }
    });

    //bind to lifecycle:
    CameraX.bindToLifecycle((LifecycleOwner) this, preview, imgCap);
}

private void updateTransform() {
    Matrix mx = new Matrix();
    float w = textureView.getMeasuredWidth();
    float h = textureView.getMeasuredHeight();

    float cX = w / 2f;
    float cY = h / 2f;

    int rotationDgr;
    int rotation = (int) textureView.getRotation();

    switch (rotation) {
        case Surface.ROTATION_0:
            rotationDgr = 0;
            break;
        case Surface.ROTATION_90:
            rotationDgr = 90;
            break;
        case Surface.ROTATION_180:
            rotationDgr = 180;
            break;
        case Surface.ROTATION_270:
            rotationDgr = 270;
            break;
        default:
            return;
    }

    mx.postRotate((float) rotationDgr, cX, cY);
    textureView.setTransform(mx);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (requestCode == REQUEST_CODE_PERMISSIONS) {
        if (allPermissionsGranted()) {
            startCamera();
        } else {
            Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}

private boolean allPermissionsGranted() {

    for (String permission : REQUIRED_PERMISSIONS) {
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

}

0 个答案:

没有答案