androidandroid-cameraandroid-camera-intent

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


I'd like to know how can I control when a pciture taken from camera can be saved, right now I have this code:

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 + "");
      }

But right now, the preview isn't working, I mean, the image is saved in PhotoAR folder, but it's saved automatically, I'd like to have a button or something, and save or discard option, is there any way to improve it to accomplish this behavior?

Any example perhaps?


Solution

  • Create a dialog box asking for save or cancel the photo just above the code(code to save a pic) and call the code you described only inside the onclick listener of ok button.

    In the following code the default camera app itself asks for save or cancel after an image capture

    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);
    }