0

I read about it but I am unable to create a folder in internal storage that i can see it.

I have tried below code

 ContextWrapper contextwrapper=new ContextWrapper(MainActivity.this);
 File file=new File(getfiledir(),"namefile");
Garf365
  • 3,619
  • 5
  • 29
  • 41

1 Answers1

2

Try this following, it's working fine.

   File file=new File(Environment.getExternalStorageDirectory()+"/folderName");

        if (!file.exists())
        {
            file.mkdirs();
        }

        String data="this is my first insert data";


        File myappFile=new File(file
                + File.separator + "myapp.txt");

        FileOutputStream fos = new FileOutputStream(myappFile);
        fos.write(data.getBytes());
        fos.close();

Must require these permissions,

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27
sunil
  • 796
  • 1
  • 8
  • 28
  • This creates a file in external storage as you can see. – greenapps Dec 21 '17 at 12:31
  • `if (!file.exists()) { file.mkdirs(); }`. Check the return value of mkdirs(). `if (!file.exists()) { if ( !file.mkdirs()){ Toast(... sorry cannot create directory ...); return;} }` – greenapps Dec 21 '17 at 12:32
  • `Must require these permissions,`. You forget requesting permissions at runtime for Android 6+. – greenapps Dec 21 '17 at 12:33