Organizing app photos into folder

Asked

Viewed 56 times

2

I have the following code snippet to define the description of the photos my app 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 in a folder with the name of my app in Android photo gallery and not along with the other photos. What should I do ?

1 answer

2


Raphael, I found this method that creates a directory and saves the file in the folder you set, note that it asks the Bitmap the image to be saved and the file name that in your case I believe 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 like File, p/ achieve the Bitmap:

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

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.