reza hemati
reza hemati

Reputation: 21

How to create File In Internal Storage?

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");

Upvotes: 0

Views: 2391

Answers (1)

sunil
sunil

Reputation: 842

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"/>

Upvotes: 2

Related Questions