How to create a directory, and save a picture to it in Android
Asked Answered
A

1

1

This is a function I have written that tries to:

  1. Create a folder with the users name
  2. Save a .jpg inside of that folder

The folder creation works fine, however when I try to save the pictures, they all save with the correct name, however they do not save in their intended folders. In other words, instead of having a folder containing a bunch of folders each containing one picture, I have one folder containing a bunch of empty folders, and a bunch of pictures all outside their folders (I can clarify if needed).

This is my code:

 public void addToDir(List<Contact> list){

        for(int i = 0; i < list.size(); i++){

            String nameOfFolder = list.get(i).getName();
            Bitmap currentBitmap = list.get(i).getBusiness_card();

            String conName = Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator +
                    "Connected Accounts" + File.separator + nameOfFolder;

            File conDir = new File(conName);

            if (!conDir.mkdirs()) {
                if (conDir.exists()) {
                } else {
                    return;
                }
            }

            try {
                FileOutputStream fos = new FileOutputStream(conName + ".jpg", true);
                currentBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                fos.flush();
                fos.close();
            } catch (Exception e) {
                Log.e("MyLog", e.toString());
            }
        }
    }

I suspect the problem is with the FileOutputStream path, but I am not sure how to set it so that it is set to the folder I just created.

Much appreciated

Alroi answered 28/4, 2016 at 13:49 Comment(2)
could you try FileOutputStream fos = new FileOutputStream(conName + File.separator+"image.jpg", true); NB:not testedJarodjarosite
This worked! Thanks for the simple yet effective solutionAlroi
D
1

This is how to define mFileTemp

String state = Environment.getExternalStorageState();
File mFileTemp;
if (Environment.MEDIA_MOUNTED.equals(state)) {
//this is like that 
//directory :  any folder name/you can add inner folders like that/your photo name122412414124.jpg
mFileTemp = new File(Environment.getExternalStorageDirectory()+File.separator+"any folder name"+File.separator+"you can add inner folders like that"
        , "your photo name"+System.currentTimeMillis()+".jpg");
mFileTemp.getParentFile().mkdirs();
}
else {
mFileTemp = new File(getFilesDir()+"any folder name"+
        File.separator+"myphotos")+File.separator+"profilephotos", "your photo name"+System.currentTimeMillis()+".jpg");
mFileTemp.getParentFile().mkdirs();

This is how i save any image

try {
        InputStream inputStream = getContentResolver().openInputStream(data.getData());
        FileOutputStream fileOutputStream = new FileOutputStream(mFileTemp);
        copyStream(inputStream, fileOutputStream);
        fileOutputStream.close();
        inputStream.close();
    } catch (Exception e) {
        Log.e("error save", "Error while creating temp image", e);
    }

And copyStream method

public static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}
Dosia answered 28/4, 2016 at 14:31 Comment(2)
This is also working. Is there a way I can check if a folder/image exists before writing to it?Alroi
you can check this question : #2625919 and you can both use my answer and this tooCindy

© 2022 - 2024 — McMap. All rights reserved.