8

I have a problem with permissions in my application. It looks like my app does not have permissions to create files on storage.

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos;

try 
{
   fos = getApplicationContext()
           .openFileOutput(FILENAME, Context.MODE_PRIVATE);     
   fos.write(string.getBytes());
   fos.close();
}
catch (Exception e)
{
   e.printStackTrace();
}

In my main class onCreate handler, and I always get:

06-02 13:23:52.996: WARN/ApplicationContext(6278): Unable to create files directory
06-02 13:23:54.203: WARN/System.err(6278): java.lang.NullPointerException
06-02 13:23:54.226: WARN/System.err(6278):     at android.app.ContextImpl.openFileOutput(ContextImpl.java:464)
06-02 13:23:54.234: WARN/ActivityManager(2470): Activity idle timeout for HistoryRecord{485ebe10 com.servision.svclient/.Main}
06-02 13:23:54.257: WARN/System.err(6278):     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
06-02 13:23:54.273: WARN/System.err(6278):     at com.servision.svclient.Main.onCreate(Main.java:59)
06-02 13:23:54.277: WARN/System.err(6278):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-02 13:23:54.285: WARN/System.err(6278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-02 13:23:54.293: WARN/System.err(6278):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 13:23:54.296: WARN/System.err(6278):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 13:23:54.304: WARN/System.err(6278):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 13:23:54.312: WARN/System.err(6278):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 13:23:54.320: WARN/System.err(6278):     at android.os.Looper.loop(Looper.java:123)
06-02 13:23:54.324: WARN/System.err(6278):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 13:23:54.332: WARN/System.err(6278):     at java.lang.reflect.Method.invokeNative(Native Method)
06-02 13:23:54.336: WARN/System.err(6278):     at java.lang.reflect.Method.invoke(Method.java:521)
06-02 13:23:54.343: WARN/System.err(6278):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-02 13:23:54.355: WARN/System.err(6278):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-02 13:23:54.359: WARN/System.err(6278):     at dalvik.system.NativeStart.main(Native Method)

in response.

I believe its something very simple in definitions of my project. But I can't figure out what it is.

If I create another project and put same code in to it, it works fine.

my manifest file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.servision.svclient"
      android:versionCode="1"
      android:versionName="1.0">
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />


  <application android:icon="@drawable/main" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".Main" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".StreamActivity" android:label="@string/app_name"/>        
    <activity android:name=".MainActivity"/>
    <activity android:name=".GatewayList"/>
    <activity android:name=".StatisticsActivity"/>
    <activity android:name=".SettingsActivity"/>
    <activity android:name=".MapsActivity"/>
    <activity android:name=".AboutActivity"/>
  </application>
  <uses-sdk android:minSdkVersion="8" />


</manifest> 
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
AlexS
  • 927
  • 4
  • 16
  • 29

3 Answers3

3

Device management => Right-Click on Device => Uninstall App => Re-run.

Akli
  • 1,351
  • 1
  • 15
  • 28
  • 1
    While this idea may feel a bit random, it is a good one. "Unable to create files directory" is probably a system-level error resulting from the device's catalog of installed apps becoming inconsistent with itself, thus **removing and reinstalling** the app may indeed be the most appropriate course of action. At least it does not seem like the problem is in the code of the application itself. – Chris Stratton May 13 '14 at 17:10
0

Is the file you are accessing definitely on the internal storage? You haven't declared the WRITE_EXTERNAL_STORAGE permission. You would need this to write to the sdcard

fleetway76
  • 1,600
  • 9
  • 12
  • 1
    yes it is, it has no path , so i assume it will be created on internal storage, and even if i add WRITE_EXTERNAL_STORAGE , it still will not work – AlexS Jun 02 '11 at 12:02
  • i solved this problem by manually removing whole application folder from device. – AlexS Jun 02 '11 at 13:14
  • openFileOutput() returns a path on the **internal** storage. – Chris Stratton Mar 25 '14 at 22:44
  • [openFileOutput()](https://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,%20int)) doesn't need any permissions to write or read the file – Quentin G. Mar 15 '17 at 13:59
0

Your FILENAME variable is wrong: what is the value of FILENAME?

That error occurs because an attempt to produce a file based off your FILENAME variable results in a null value inside openFileOutput. So FILENAME is not producing a valid file.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 1
    value of FILENAME is "file_ouput", but if i change it to hardcoded string, say "test", it does not matter , it will still fail. – AlexS Jun 02 '11 at 11:59
  • 1
    i solved this problem by manually removing whole application folder from device. – AlexS Jun 02 '11 at 13:14
  • Ah, did you change the signing certificate? If you sign the app with a different certificate (for example, if your debug cert expires and you generate a new one or if you use a release cert) then the ownership on the underlying directory will change. The null pointer is a weird error, though... – Femi Jun 02 '11 at 14:34
  • 1
    I had the same issue, not being able to use getFilesDir() as it always returned null. I was able, however, to use getDir, but when trying to write any file, another error occured. For some reason the app's folders didn't seem to get the right permission at install time. Notice that this didn't happen on all devices I had, I only was able to see this on a G1, but it was enough to waste two hours of my time. I also had a similar application that was able to use getFilesDir without any issues, so slowly I got to this post, where AlexS's comment was the solution. Thanks for that! – radhoo May 12 '12 at 14:35
  • @AlexS: If you solved the problem, please either post your solution and accept it to close the question or remove the question – Mike T Oct 26 '12 at 08:59