-1

I mean I want to define a file path on phone memory not sdcard.

I want to use this :

FileOutputStream out3 = new FileOutputStream(Environment.getRootDirectory()+"/a/b.txt"); 

this gives error.
How can i fix this? thanks.

error

 09-26 19:49:11.050: E/AndroidRuntime(20214): FATAL EXCEPTION: main
09-26 19:49:11.050: E/AndroidRuntime(20214): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kelime.tekerrur/com.example.kelime.tekerrur.MainActivity}: java.lang.NullPointerException
09-26 19:49:11.050: E/AndroidRuntime(20214):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
09-26 19:49:11.050: E/AndroidRuntime(20214):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-26 19:49:11.050: E/AndroidRuntime(20214):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
09-26 19:49:11.050: E/AndroidRuntime(20214):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
09-26 19:49:11.050: E/AndroidRuntime(20214):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 19:49:11.050: E/AndroidRuntime(20214):    at android.os.Looper.loop(Looper.java:137)
CompEng
  • 7,161
  • 16
  • 68
  • 122

2 Answers2

2

To get the SD-Card path you have to use Environment.getExternalStorageDirectory()
The Environment.getRootDirectory() which Mukesh suggested returns the device root "/" - you can't write (or read) in this Directory on a non-rooted-device.

Thommy
  • 5,070
  • 2
  • 28
  • 51
1

You can try this -

File imgFile = new File(Environment.getRootDirectory()+“/test_image.jpg”);
Mukesh Soni
  • 6,646
  • 3
  • 30
  • 37