仅在用户选择"确定"

时间:2017-11-13 04:54:25

标签: java android android-fragments camera

因此本规范的工作原理是,当我打开相机时拍一张照片"重试" &安培; "确定"屏幕出现,这很好但是当我点击"重试"相机返回拍摄模式(拍照)图像仍然会加载到位图中,并在我按后退键退出相机时将图像保存到目录。

单击“重试”并退出相机时,有没有办法不保存图像? 或者仅在我选择" OK"?

时保存

我正在使用此相机API ParaCamera 或者我应该在相机api中使用android build?如果是这样的话

public void takePhoto() {
    checkStoragePermissions();
    SharedPreferences unID = PreferenceManager.getDefaultSharedPreferences(getContext());
    imgID = unID.getString("unique_ID", "");
    camera = new Camera.Builder()
            .resetToCorrectOrientation(true)
            .setTakePhotoRequestCode(1)
            .setDirectory("/Pictures/WasThere/Images")
            .setName(imgID)
            .setImageFormat(Camera.IMAGE_JPEG)
            .setCompression(75)
            .setImageHeight(700)
            .build(this);
    try {
        camera.takePicture();
    }catch (Exception e){
        e.printStackTrace();
    }
}
    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == Camera.REQUEST_TAKE_PHOTO){
        Bitmap bitmap = camera.getCameraBitmap();
        if(bitmap != null) {
            captureImage.setImageBitmap(bitmap);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // this makes the image disapear in 6 Seconds
                    captureImage.setImageResource(R.color.transpar);
                }
            },6000);
            storeImage(bitmap);
        }else{
            Toast.makeText(getContext().getApplicationContext(),"Picture not taken!",Toast.LENGTH_SHORT).show();
        }
    }
}

private void storeImage(Bitmap image) {
    File pictureFile = getOutputMediaFile();
    if (pictureFile == null) {
        Log.d(TAG,
                "Error creating media file, check storage permissions: ");// e.getMessage());
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.JPEG, 75, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
}

private  File getOutputMediaFile(){
    // To be safe, you should check that the SDCard is mounted
    //captureImage.setImageBitmap(camera.getCameraBitmap());
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory().toString()
            + "/Pictures/WasThere");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.
    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("_dd_MM_yyyy_k:mm:ss").format(new Date());
    File mediaFile;
    String mImageName=imgID +timeStamp+".jpg";
    mediaFile = new File(mediaStorageDir.getPath()+File.separator + mImageName);
    //Gets the image current path
    mCurrentPhotoPath = mediaFile.getAbsolutePath();
    galleryAddPic();
    return mediaFile;

   }

1 个答案:

答案 0 :(得分:0)

您可以将onActivityResult中的代码更改为

if(requestCode == CAMERA_REQUEST_CODE&& resultCode == Activity.RESULT_OK ){