0

The picture captured using camera in my application is saved in a folder in External Storage.When I check the folder I found out that it's quality is low compared to the same image in the gallery.Using FileOutputStream I save the image to the folder.Is there any other way where I could save the image without losing the quality?Below is my code:

Code to save captured Image to Folder:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode==2&&resultCode==RESULT_OK)
            {
                File camerafile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/CameraTestFile");
                if (!camerafile.exists())
                {
                    camerafile.mkdir();
                    Toast.makeText(getApplicationContext(),"Folder created",Toast.LENGTH_SHORT).show();
                }
                else
                    {
                    Toast.makeText(getApplicationContext(),"Folder already exists ",Toast.LENGTH_SHORT).show();
                }
                Bitmap bitmap;
                bitmap= (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);

                try {

                    FileOutputStream fileOutputStream=new FileOutputStream(camerafile+"/camera3.png");
                    fileOutputStream.write(byteArrayOutputStream.toByteArray());
                    fileOutputStream.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                }
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jobin
  • 1,489
  • 5
  • 27
  • 52
  • Possible duplicate of [Android Camera Intent: how to get full sized photo?](https://stackoverflow.com/questions/6448856/android-camera-intent-how-to-get-full-sized-photo) – ADM Jun 19 '18 at 15:06
  • Also read https://developer.android.com/training/camera/photobasics. – ADM Jun 19 '18 at 15:07

1 Answers1

0

Refer to my answer on https://stackoverflow.com/a/50929913/9882512>

You can use the cod. Just change the path of file. It will directly save the file to the desired location and there will be no need to copy the file

 File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                        String pictureName = getPictureName();
                        imagefile = new File(pictureDirectory, pictureName);
                        picUri = Uri.fromFile(imagefile);
                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);