0

I am most beginner in android studio.I want to build a camera app just simply take photo and save to sd card but the app did't working properly.The photo is not saving to phone / sd card.And i also give permission to the xml file.When I run my app the app can take photo but don't saving it. Below is my code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.my_image);
        take_photo = (Button) findViewById(R.id.take_photo);
        take_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(flag == 0) {

                    // -- code for taking photo --

                    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, requestCode);

                } else if(flag == 1) {

                    //--- code for saving photo ---

                    savePhotoToMySdCard(b);

                    Toast.makeText(getApplicationContext(), "Photo saved to sd card!", Toast.LENGTH_SHORT).show();

                    flag = 0;
                    take_photo.setText("Take Photo");

                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(this.requestCode == requestCode && resultCode == RESULT_OK && data != null){

            b = (Bitmap) data.getExtras().get("data");

            iv.setImageBitmap(b);

            flag = 1;
            take_photo.setText("Save Photo");

        }

    }
    private void savePhotoToMySdCard(Bitmap bit){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
        String pname = sdf.format(new Date());
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File folder = new File(root+"/SCC_Photos");
        folder.mkdirs();

        File my_file = new File(folder, pname+".jpeg");
        if(my_file.exists())
            my_file.delete();

        try {

            FileOutputStream stream = new FileOutputStream(my_file);
            bit.compress(Bitmap.CompressFormat.JPEG, 90, stream);
            stream.flush();
            stream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1
    Have you given the Runtime Permission for read and write external storage? – Shubham Sejpal Jul 05 '18 at 06:37
  • 1
    Possible duplicate of [How to capture the image and save it in sd card](https://stackoverflow.com/questions/5661424/how-to-capture-the-image-and-save-it-in-sd-card) – Aseem Sharma Jul 05 '18 at 06:46

2 Answers2

2
    take_photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(flag == 0) {
                // -- code for taking photo --
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, requestCode);
            } else if(flag == 1) {
                int check = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (check != PackageManager.PERMISSION_GRANTED)
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                else {
                    //--- code for saving photo ---
                    savePhotoToMySdCard(b);
                    Toast.makeText(getApplicationContext(), "Photo saved to sd card!", Toast.LENGTH_SHORT).show();
                    flag = 0;
                    take_photo.setText("Take Photo");
                }
            }
        }
    });
Vinay Rathod
  • 1,262
  • 1
  • 10
  • 19
0

You may try this:-

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());


                        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Directory_name");
                        imagesFolder.mkdirs();

                        File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
                        if(Build.VERSION.SDK_INT>=24) {
                            picUri = FileProvider.getUriForFile(UserPhoneNumber.this, getPackageName() + ".provider", image);
                        }
                        else {
                            picUri = Uri.fromFile(image);
                        }


                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);
diksha
  • 109
  • 8