开发者

Why isn't the my root folder created?

I tried to create an external storage public directory in my main root directory and wrote the following lines of code. The code compiles properly, and shared preferences are updated correctly, but the root folder is not created in the device.

I use Atrix (it has this Internal Storage and no SD card). I also tried downloading the Atrix Addon for Gingerbread, and I tried compiling this in the emulator and the program crashes. (But it works perfectly well in real device.)

  1. Why is the emulator crashing (I have 1 GB SD card in emulator)? (It crashes with a null pointer exception)

  2. why is the folder not created in the real device?

Device Configurations:

MinSDK - 8 SDK version - 10

Code Snippet

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ixfer_main);

        File rootDirectory = new File(getExternalFilesDir(null),"/AppRoot");
        rootDirectory.mkdirs(); // also tried using mkdir() - still no g开发者_StackOverflowood

EDIT: It was the permissions issue. Fixed and works now.

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


to complete previous answer:

do not forget to check External Storage State before doing anything on it.

  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  /* do job on external storage */
  }


In order to access the file system, you must have the proper permission added to your android manifest xml file. Without the appropriate permissions, your file object won't be created and you'll get a null reference.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app.myapp" >
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜