4

I would like to save my images in a folder that corresponds to the name of my app, instead of the default "Pictures" folder. Here's my code in onRequestPermissionsResult:

if(requestCode == SAVE_IMAGE) {
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                try {
                    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + R.string.app_name + "/";
                    File dir = new File(path);
                    if(!dir.exists()){
                        dir.mkdirs();
                    }

                    OutputStream out;
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

                    File photoFile = new File(path, timeStamp + ".jpg");
                    photoFile.createNewFile();

                    out = new FileOutputStream(photoFile);

                    Bitmap full_image_result = full_image.copy(full_image.getConfig(), true);
                    selectedFilter.apply(full_image_result);
                    full_image_result.compress(Bitmap.CompressFormat.JPEG, 90, out);

                    MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(),photoFile.getAbsolutePath(), photoFile.getName(), photoFile.getName());

                    out.flush();
                    out.close();

                    Toast.makeText(getApplicationContext(),R.string.savedMessage, Toast.LENGTH_LONG).show();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplicationContext(), R.string.noPermissions, Toast.LENGTH_LONG).show();
            }
        }
Rukka
  • 350
  • 1
  • 9
  • "I would like to save my images in a folder that corresponds to the name of my app, instead of the default "Pictures" folder" -- then delete the `insertImage()` call. The rest of your code already does what you want. – CommonsWare Feb 24 '20 at 12:55
  • If I do that the image doesn't show up in the gallery... – Rukka Feb 24 '20 at 13:27
  • Also, sometimes the images go in a folder in the gallery called "2131558439" for some reason – Rukka Feb 24 '20 at 13:28
  • "If I do that the image doesn't show up in the gallery" -- https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Feb 24 '20 at 13:29
  • I can see it in the gallery now, but in a folder called "2131558439", still not the result I want – Rukka Feb 24 '20 at 13:40
  • Problem solved, for some reason the path was wrong, I replaced it with `Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictura/"` – Rukka Feb 24 '20 at 13:47

1 Answers1

2

Somehow the path was wrong, replaced it with Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictura/" and now it's working. I also replaced the insertImage call with

MediaScannerConnection.scanFile(this, new String[]{photoFile.getAbsolutePath()}, new String[]{"image/jpg"}, null);

, as CommonsWare suggested.

Rukka
  • 350
  • 1
  • 9