views:

11

answers:

1

I wrote some code to save images on the SD Card a while back. Now I added the targetSDKVersion to my manifest and now my file saving code ceased to work.

I can reproduce it through removing the targetSdkVersion from my manifest from that on my App won't write anything to the SD Card.

Is there an API change between Android 1.5 and 1.6 that prevents me from writing to the SD-Card?

File imageDirectory = new File(Environment.getExternalStorageDirectory()
                 .getAbsolutePath()
                 + File.separator
                 + FOLDER_NAME);

Log.d(ImageSaver.class.getSimpleName(), "SD Card status: "
            + Environment.getExternalStorageState());

if (!imageDirectory.exists()) {
   boolean created = imageDirectory.mkdir();
   Log.d(ImageSaver.class.getSimpleName(), "Created image directory "
                + imageDirectory + " " + created);
}
File imageFile = new File(imageDirectory.getAbsolutePath() + File.separator
                    + name + nameSuffix + FILE_ENDING);

bitmap.compress(Bitmap.CompressFormat.PNG, FULL_QUALITY,
         new FileOutputStream(imageFile));

This is the test code for compressing an bitmap to the SD Card. With the following manifest entry it does not work:

<uses-sdk
   android:minSdkVersion="3"
   android:targetSdkVersion="8" />

I get the following exception:

09-27 11:35:58.689: ERROR/ImageSaver(8672): File not found 09-27 11:35:58.689: ERROR/ImageSaver(8672): java.io.FileNotFoundException: /sdcard/FOLDER/1285580158662.png

Removing the targetSdkVersion makes it work on all platforms again.

How can I make the code run with the targetSdkVersion set?

+1  A: 

You have to add the permission WRITE_EXTERNAL_STORAGE to your manifest - it is new since API level 4.

mreichelt
Sorry I forgot to add the Exception. I get a File not found Exception for the file I use to initialize the FileOutputStream with. A missing permission would give me another exception I think.
Janusz
Okay it would not. Thank you it works now :) The exception totally lead me on the wrong track.
Janusz