0

I can't seem to be able to figure out how to create a directory/file through an android app to the internal storage. I have the following code:

public class Environment extends SurfaceView implements SurfaceHolder.Callback {
    public static String FILE_PATH;
    //other unimportant variables

    public Environment(Conext context) {
        super(context);
        FILE_PATH = context.getFilesDir() + "/My Dir/";
        File customDir = new File(FILE_PATH);
        if(!customDir.exists())
            System.out.println("created my dir: " + customDir.mkdir());

        File test = new File(FILE_PATH + "testFile.txt");
        try {
            if(!test.exists())
                System.out.println("created test: " + test.createNewFile());
        } catch(Exception e) {
            e.printStackTrace();
        }
        //other unimportant stuff
    }
}

I then use ES File Explorer to see if it created the file and I don't see the directory/file anywhere despite it printing out "true" for the System.out.println() calls.

What am I doing wrong?

Ryan
  • 727
  • 2
  • 14
  • 31

2 Answers2

1

The path where you are creating file is in apps private location. Generally you can't access it from outside. It's actually created in apps data folder. However it seems you want to write in external folder. To write in the external storage, you must request the WRITE_EXTERNAL_STORAGE permission in your manifest file:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

code:

    String folder_main = "My Dir";

    File f = new File(Environment.getExternalStorageDirectory(), folder_main);
    if (!f.exists()) {
        f.mkdirs();
    }
    File test = new File(f , "testFile.txt");

Here you will find how to you will create folder/file in external storage.

Save a File on External Storage

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • Still can't find the the directory, and now it says that it couldn't create the directory: "create my dir: false". Also get a "java.io.IOException: Permission Denied" when trying to create the test file – Ryan Jul 30 '17 at 04:46
  • Notice the upper portion code of this answer where permission is being given to your app. – ashubuntu Jul 30 '17 at 04:54
  • @ashubuntu Yea I added that to the manifest, still isn't working – Ryan Jul 30 '17 at 05:36
  • 1
    > "create my dir: false". Also get a "java.io.IOException: Permission Denied". So obviously problem is related to permission. Do you grant permission explicitly? If not allow it from `settings>app>yourapp>permission`. @Ryan – MD Ruhul Amin Jul 30 '17 at 08:20
  • I explicitly grant permissions in the manifest: `` and I also went to the settings and gave storage permissions in there and it still can't create the directory. I also tried changing the directory to: `File customDir = new File(android.os.Environment.getExternalStorageDirectory(), "My Dir");` which also didn't work – Ryan Jul 30 '17 at 08:33
  • Ok, I don't know what I did differently, but while hopelessly retrying the same things over and over again, something clicked and it's now working. Thanks for all of the help – Ryan Jul 30 '17 at 08:41
  • Actually it turns out manually going into the settings and giving the app storage permissions fixed my problem. Is there anyway I can do this automatically though? – Ryan Jul 30 '17 at 08:45
  • would you please try solution I provided above. – AndroidDevUser Jul 30 '17 at 11:31
  • So my solution works? As far privacy policy, android gives those authority to the users. You can search "how to seek permission on android" and will find a lot of resources. @Ryan – MD Ruhul Amin Jul 30 '17 at 12:30
  • Yea I had to look into requesting permission for read and writing to storage and everything is working fine now. Thanks again. – Ryan Jul 30 '17 at 23:11
1

You can try with below:

ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(getFilesDir().getName(), Context.MODE_PRIVATE);
File file =  new File(directory,”fileName”);
String data = “TEST DATA”;
FileOutputStream fos = new FileOutputStream(“fileName”, true); // save
fos.write(data.getBytes());
fos.close();

This will write file in Device's internal storage (/data/user/0/com.yourapp/)

Hope this helps!