Organizing photos of the application in folder

2

I have the following code snippet to define the description of the photos that my application will take and where the photos will be saved.

private File criarArquivo() throws IOException {
   String descricao = new SimpleDateFormat("dd_MM_yyyy_HH:mm:ss").format(new Date());
   File pasta = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
   File imagem = new File(pasta.getPath() + File.separator + "DiabetesMonitor_" + descricao + ".jpg");
   return imagem;
}

I would like to save these photos to a folder with the name of my app in the Android photo gallery and not along with the other photos. What should I do?

    
asked by anonymous 18.04.2017 / 16:34

1 answer

2

Raphael, I found this method that creates a directory and saves the file in the folder that you set, note that it asks for the Bitmap of the image to be saved and the file name that in your case I believe which would be the variable descricao :

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/nomeDoApp");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/nomeDoApp/");
        wallpaperDirectory.mkdirs();
    }

    File file = new File(new File("/sdcard/nomeDoApp/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

In your method you have an object of type File , to get the Bitmap :

File imagem = new File(pasta.getPath() + File.separator + "DiabetesMonitor_" + descricao + ".jpg");

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

Now, just adapt it for your code, I could not test it, but I believe this is the way, I hope it helps.

Sources: Save images in an specific folder - file to bitmap

    
18.04.2017 / 17:01