简体   繁体   中英

Creating an internal folder in android

I'm trying to create a folder in android to store files concerning the user of the application.

I created an input output utility class for the use of the application.

For the moment i wrote this:

public void createFolder(){

    String dirPath =  new String();
    dirPath  =  getFilesDir().getAbsolutePath() + File.separator + "MyFolder";
    File folder = new File(dirPath);

    if(!folder.exists()) {
        folder.mkdir();
        Log.i("IO", ""+folder.exists());
    }

}

But when i try and call it from the main activity i get a null pointer exception: "java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference"

I know what is a null object reference but i don't understand which part of my code cause it.

You should add permission in manifest.xml

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

Save Files on Device Storage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM