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

无人久伴 提交于 2019-12-24 13:47:43

问题


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


回答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);
    }
}


来源:https://stackoverflow.com/questions/36916703/how-to-create-a-directory-and-save-a-picture-to-it-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!