如何在 Android 中手动保存图像(使用相机拍摄)

How to save the image (taken using camera) manually in Android

我想知道如何控制何时可以保存从相机拍摄的照片,现在我有这个代码:

File storagePath = new File(Environment.
                    getExternalStorageDirectory() + "/PhotoAR/"); 
      storagePath.mkdirs(); 
   
      File myImage = new File(storagePath,
                    Long.toString(System.currentTimeMillis()) + ".jpg");
            
      try
      {
        FileOutputStream out = new FileOutputStream(myImage);
        newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


        out.flush();
        out.close();
      }
      catch(FileNotFoundException e)
      {
        Log.d("In Saving File", e + "");    
      }
      catch(IOException e)
      {
        Log.d("In Saving File", e + "");
      }

但是现在,预览不起作用,我的意思是,图像保存在 PhotoAR 文件夹中,但它是自动保存的,我想要一个按钮或其他东西,然后保存或discard 选项,是否有任何方法可以改进它以实现此行为?

有什么例子吗?

在代码(保存图片的代码)上方创建一个对话框,要求保存或取消照片,并仅在确定按钮的 onclick 侦听器中调用您描述的代码。

在以下代码中,默认相机应用程序本身会在图像捕获后要求保存或取消

mCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (taken_image == null) {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
                    image_uri = fileUri;
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                    startActivityForResult(intent, 1);
                    mEdit.setVisibility(View.VISIBLE);
                    dialog.cancel();
                }

            }
        });

@Override
    public void onActivityResult(int requestCode, int resultCode,
            final Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

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

            if (image_uri != null)
                try {
                    ExifInterface exif = new ExifInterface(image_uri.getPath()); // Since API Level 5
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
                    // TODO
                    upload_image_hd = BitmapFactory.decodeFile(image_uri
                            .getPath());
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        Rotate90Bitmap(upload_image_hd, 90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        Rotate180Bitmap(upload_image_hd, 180);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        Rotate270Bitmap(upload_image_hd, 270);
                        break;
                    case ExifInterface.ORIENTATION_NORMAL:
                        RotateBitmap(upload_image_hd, 0);
                        break;
                    default:
                        RotateBitmap(upload_image_hd, 0);
                        break;
                    }

                } catch (OutOfMemoryError e) {
                    Toast.makeText(getActivity(),
                            e + "\"memory exception occured\"",
                            Toast.LENGTH_LONG).show();

                    taken_image = null;
                    round_Image = null;
                    upload_image_hd = null;

                } catch (Exception e) {
                    Toast.makeText(getActivity(), e + "\"exception occured\"",
                            Toast.LENGTH_SHORT).show();
                    taken_image = null;
                    round_Image = null;
                    upload_image_hd = null;

                }

public Bitmap RotateBitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
                Base64.NO_WRAP);
        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
    }

    public Bitmap Rotate90Bitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
                Base64.NO_WRAP);
        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
    }

public Bitmap Rotate180Bitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
    // get the base 64 string
    imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
            Base64.NO_WRAP);

    uploadProfilePicture_bytes();
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
}

public Bitmap Rotate270Bitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
    // get the base 64 string
    imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
            Base64.NO_WRAP);

    uploadProfilePicture_bytes();
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
}