Take picture and save it on internal storage

user3159043 picture user3159043 · Sep 20, 2016 · Viewed 9.6k times · Source

I'm trying to take a picture and save it in the internal storage so this way, only my app will have access to the pictures how can i do this? I'm trying a lot of things without success, even the official documentation from google is broken, here's my actual code, that saves on public directory:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pictures);

        imageView = (ImageView)findViewById(R.id.imageView);
        fabNewPicture = (FloatingActionButton)findViewById(R.id.fab_new_picture);
        fabNewPicture.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                takePicture(v);
            }
        });

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
        }
    }

    private void takePicture(View v)
    {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        file = Uri.fromFile(getOutputMediaFile());
        i.putExtra(MediaStore.EXTRA_OUTPUT, file);

        startActivityForResult(i, 100);
    }

    private static File getOutputMediaFile()
    {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "FotoAula");

        if (!mediaStorageDir.exists()){
            if (!mediaStorageDir.mkdirs()){
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100) {
            if (resultCode == RESULT_OK) {
                imageView.setImageURI(file);
            }
        }
    }

But this way the picture will become public and available even on the gallery, I don't want that, because I need to add some database info with each picture, so I need them to be private to my app...

How can i do this?

Thank you!

Answer

Yorbenys Pardo Rodríguez picture Yorbenys Pardo Rodríguez · Sep 20, 2016

If you save your picture on SD card or external storage any app can access it. Try to get the private directory and save your photo there.

Take from here

To create and write a private file to the internal storage:

1-Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.

2-Write to the file with write().

3-Close the stream with close().

For example:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

Read "Using the Internal Storage" on: Link