Вопрос

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 good

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

<uses-permission android:name="android.permission.WRITE_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>

Другие советы

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 */
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top