0

I have a fragment that displays a camera and, when the user clicks the main button, one photo is taken. I tried to create a path in the gallery and save images there but every time I open the gallery in my emulator, there is no folder with the images. Do you know what may be causing this problem? This is the code I have:

Code to create the path and save images

private void saveImage(Bitmap finalBitmap, String image_name) {

   final String appDirectoryName = "/Feel/";
   String root = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES).toString() + appDirectoryName;
     File myDir = new File(root);
    myDir.mkdirs();
    String fname = "Image-" + image_name + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    Log.i("LOAD", root + fname);
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Turma RC
  • 71
  • 1
  • 11
  • Possible duplicate of [how to save bitmap to android gallery](https://stackoverflow.com/questions/36624756/how-to-save-bitmap-to-android-gallery) – Muhammad Usman Jun 27 '18 at 14:54
  • @MuhammadUsman ahah i saw this code from there – Turma RC Jun 27 '18 at 14:55
  • and I still can't make it so... no duplicate – Turma RC Jun 27 '18 at 14:55
  • show your logcat – Muhammad Usman Jun 27 '18 at 14:58
  • @MuhammadUsman https://pastebin.com/hWRD23y6 here – Turma RC Jun 27 '18 at 15:04
  • What Android OS version are you running? – Dracarys Jun 27 '18 at 15:13
  • It says in emulator it's Android 7.1.1 – Turma RC Jun 27 '18 at 15:14
  • I do believe you still need to write the contents of the Output Stream to disk. compress() does not do that for you - it merely compresses the contents to the stream which still needs to be written. – CmosBattery Jun 27 '18 at 15:14
  • @CmosBattery Can you be a little more specific? I don't understand much about this FILE's methods ahah – Turma RC Jun 27 '18 at 15:15
  • https://developer.android.com/training/data-storage/files <- this is just easier than duplicating everything in a comment or even an answer =) Take note of outputStream.write() – CmosBattery Jun 27 '18 at 15:18
  • Actually, I've read that. I just couldn't understand the write the files of the Output Stream to disk. I mean, I made everything that the question that is linked to this had and I still couldn't create. I have all permissions – Turma RC Jun 27 '18 at 15:20
  • For one you're not using the write() method =) – CmosBattery Jun 27 '18 at 15:21
  • @CmosBattery Ohh, that was easier ahah I got you! I need to add out.write() but what do I add inside that method? – Turma RC Jun 27 '18 at 15:23
  • I could be wrong... going to try myself right now... – CmosBattery Jun 27 '18 at 15:31
  • @CmosBattery Ok. Thanks for the help btw! – Turma RC Jun 27 '18 at 15:32
  • never mind compress() does actually also write the stream to file too. So l will look for something else... – CmosBattery Jun 27 '18 at 15:43
  • @CmosBattery Yeah, in this meanwhile I've read that compress does create the file too. I'm still trying to resolve this too ;) – Turma RC Jun 27 '18 at 15:45
  • Its funny, I have one app that writes to /download and right now I can't figure out what's different lol – CmosBattery Jun 27 '18 at 16:21
  • Oh, I think that's what it is, you need to use the paths properly such as new file = File(newLocalPath, queuedFileName) such as you had then FileOutputStream(file) BUT you also need to check permissions from code too. The app that I was testing in is using app cache so I don't have them set up here. Without that you will get permission denied. And with paths incorrect you will get file not found or whatever error (which I think was happening when I first ran your code). https://developer.android.com/training/permissions/requesting – CmosBattery Jun 27 '18 at 16:39
  • So, you're saying that I need to set permissions on run time and change my `File file = new File(putHereMyRoot, putHereMyFile)`?? – Turma RC Jun 27 '18 at 19:42
  • @CmosBattery I found the problem. While debugging I found out that it can't compile these lines `finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);` I don't know why, but it goes redirected to the "catch". What may be causing this? – Turma RC Jun 28 '18 at 20:34

1 Answers1

0

Use this code instead

private void saveImage(Bitmap finalBitmap, String image_name) {
     try {

            FileOutputStream outStream = null;
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
            dir.mkdirs();
            String fileName = String.format("%d.jpg", image_name);
            File outFile = new File(dir, fileName);
            outStream = new FileOutputStream(outFile);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
                Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
}

and don't forget to add the permission to write and read from the external storage
see this for more details Permission denied on writing to external storage despite permission

Abdulmalek Dery
  • 996
  • 2
  • 15
  • 39