no permission to create storage

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> 

设备管理=>右键点击设备=>卸载应用程序=>重新运行。


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


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.

链接地址: http://www.djcxy.com/p/7364.html

上一篇: 自由格式动态web.config区域

下一篇: 没有权限创建存储