我正在实施一种备份方法来将应用程序数据库复制到 SDCard。
这在模拟器中运行良好,但是当我尝试在手机上运行它时它会崩溃。以下是相关的堆栈跟踪:
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)
我的第一个想法是权限(由于堆栈跟踪中的权限被拒绝),但我仔细检查了我的清单,它确实包含:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果它是方法中的东西:
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");
}
}
除了从 Eclipse 运行程序之外,我还需要做些什么来在手机上启用该权限吗?
编辑
根据要求的整个清单:
<?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>
如果它很重要,我正在使用的手机是摩托罗拉 Droid X2(它对我的上一期很重要,所以我想我会提到它)。