Barak
Barak

Reputation: 16393

Permissions on phone while debugging?

I'm implementing a backup method to copy the app database to the SDCard.

This works fine in the emulator, but when I try to run it on my phone it crashes. Following is the relevant stacktrace:

W/KeyCharacterMap(15814): Can't open keycharmap file
W/KeyCharacterMap(15814): Error loading keycharmap file '/data/usr/keychars/tegra-kbc.kcm.bin'.     hw.keyboards.0.devname='tegra-kbc'
W/System.err(15814): java.io.FileNotFoundException: /mnt/sdcard/GroceryList.bak (Permission denied)
W/System.err(15814):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
W/System.err(15814):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
W/System.err(15814):    at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
W/System.err(15814):    at java.io.FileOutputStream.<init>(FileOutputStream.java:66)

My first thought was permissions (due to the permission denied in the stacktrace), but I double checked my manifest and it does contain:

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

In case it's something in the method:

public void backup() {
    try {
        File sdcard = Environment.getExternalStorageDirectory();
        File outputFile = new File(sdcard,
                "GroceryList.bak");
        File data = Environment.getDataDirectory();
        File inputFile = new File(data,
                "data/cdc.workshopapps.grocerylist/databases/GroceryList");
        InputStream input = new FileInputStream(inputFile);
        OutputStream output = new FileOutputStream(outputFile);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
        output.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw new Error("Copying Failed");
    }
}

Is there something I need to do to enable that permission on the phone other than Run the program from Eclipse?

EDIT

Entire manifest as requested:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cdc.workshopapps.grocerylist"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".GroceryListMain"
            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>
    </application>
</manifest>

And in case it matters, the phone I'm using is a Motorola Droid X2 (it mattered on my last issue, so I thought I'd mention it).

Upvotes: 1

Views: 566

Answers (3)

code_finder
code_finder

Reputation: 1370

Are you sure this file already exists? sometimes just creating the file object (new File(..)) does not create the file itself. If FileNotFoundException is occurring means, then file just doesn't exist yet. You can create it by calling file.createNewFile().

 File f= new File(sdcard, "GroceryList.bak");
   if(!f.exists())
   f.createNewFile();

Upvotes: 0

user1378730
user1378730

Reputation: 930

My comment "Google said the SD card shouldn't be mounted." was the correct answer according to OP.

Upvotes: 2

IncrediApp
IncrediApp

Reputation: 10343

I think your problem is that the file doesn't exist... Try creating it first:

 File outputFile = new File(sdcard, "GroceryList.bak");
 if (!outputFile.exists())
     outputFile.createNewFile();

Upvotes: 1

Related Questions