user7569064
user7569064

Reputation:

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.

Upvotes: 1

Views: 92

Answers (1)

nhonnq
nhonnq

Reputation: 184

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

Upvotes: 1

Related Questions