问题
I need again your help please!!
I've an android application that write/read files to/from External memory. I've written all riquired permissions in AndroidManifest but I still get an error access denied.
Hier my code:
private static File convertStreamToFile(InputStream is) throws IOException {
String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
// I have tried this one too but it didn't work!!
// File root = new File (Environment.getExternalStorageDirectory(),"ekg_daten.zip");
File root = new File(dir+ "/ekg_daten.zip");
if (!root.exists())
{
root.mkdirs();
}
// File zippedDaten = new File(root, "outPut.zip");
ZipInputStream zin = new ZipInputStream(is);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
FileOutputStream fout = new FileOutputStream(root);
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.praktikum.androidcrestclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS" >
</uses-permission>
and my logcat
10-12 18:33:24.297: I/ActivityManager(79): Force stopping package com.praktikum.androidcrestclient uid=10034
10-12 18:33:24.537: I/installd(34): move /data/dalvik-cache/data@app@com.praktikum.androidcrestclient-1.apk@classes.dex -> /data/dalvik-cache/data@app@com.praktikum.androidcrestclient-1.apk@classes.dex
10-12 18:33:24.537: D/PackageManager(79): New package installed in /data/app/com.praktikum.androidcrestclient-1.apk
10-12 18:33:24.537: W/PackageManager(79): Unknown permission android.permission.WRITE_INTERNAL_STORAGE in package com.praktikum.androidcrestclient
10-12 18:33:24.790: I/ActivityManager(79): Force stopping package com.praktikum.androidcrestclient uid=10034
10-12 18:33:24.887: D/dalvikvm(79): GC_EXPLICIT freed 662K, 59% free 4335K/10375K, external 3511K/3903K, paused 67ms
10-12 18:33:24.947: D/dalvikvm(228): GC_EXPLICIT freed 4K, 51% free 2885K/5831K, external 5876K/7285K, paused 79ms
10-12 18:33:25.077: W/RecognitionManagerService(79): no available voice recognition services found
10-12 18:33:25.187: D/dalvikvm(235): GC_EXPLICIT freed 85K, 53% free 2755K/5767K, external 1625K/2137K, paused 228ms
10-12 18:33:25.277: D/dalvikvm(79): GC_EXPLICIT freed 216K, 59% free 4288K/10375K, external 3511K/3903K, paused 87ms
10-12 18:33:25.297: I/installd(34): unlink /data/dalvik-cache/data@app@com.praktikum.androidcrestclient-2.apk@classes.dex
10-12 18:33:25.307: D/AndroidRuntime(1938): Shutting down VM
10-12 18:33:25.317: D/dalvikvm(1938): GC_CONCURRENT freed 101K, 72% free 295K/1024K, external 0K/0K, paused 1ms+1ms
10-12 18:33:25.328: D/jdwp(1938): Got wake-up signal, bailing out of select
10-12 18:33:25.328: D/dalvikvm(1938): Debugger has detached; object registry had 1 entries
10-12 18:33:25.858: D/AndroidRuntime(1952): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
10-12 18:33:25.858: D/AndroidRuntime(1952): CheckJNI is ON
10-12 18:33:26.487: D/AndroidRuntime(1952): Calling main entry com.android.commands.am.Am
10-12 18:33:26.517: I/ActivityManager(79): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.praktikum.androidcrestclient/.MainActivity } from pid 1952
10-12 18:33:26.547: I/ActivityManager(79): Start proc com.praktikum.androidcrestclient for activity com.praktikum.androidcrestclient/.MainActivity: pid=1960 uid=10034 gids={3003, 1015}
10-12 18:33:26.587: D/AndroidRuntime(1952): Shutting down VM
10-12 18:33:26.607: D/dalvikvm(1952): GC_CONCURRENT freed 103K, 69% free 319K/1024K, external 0K/0K, paused 7ms+2ms
10-12 18:33:26.648: I/AndroidRuntime(1952): NOTE: attach of thread 'Binder Thread #3' failed
10-12 18:33:26.670: D/dalvikvm(1952): Debugger has detached; object registry had 1 entries
10-12 18:33:27.637: I/ActivityManager(79): Displayed com.praktikum.androidcrestclient/.MainActivity: +1s98ms
10-12 18:33:28.857: D/dalvikvm(157): GC_EXTERNAL_ALLOC freed 40K, 44% free 3469K/6151K, external 6952K/7268K, paused 44ms
10-12 18:33:32.627: W/KeyCharacterMap(157): No keyboard for id 0
10-12 18:33:32.627: W/KeyCharacterMap(157): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-12 18:33:33.006: D/dalvikvm(389): GC_EXPLICIT freed 7K, 54% free 2537K/5511K, external 1625K/2137K, paused 311ms
10-12 18:33:34.257: D/getrespond()(1960): service calling
10-12 18:33:34.726: W/System.err(1960): java.io.FileNotFoundException: /mnt/sdcard/ekg_daten.zip (Permission denied)
10-12 18:33:34.736: W/System.err(1960): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
10-12 18:33:34.736: W/System.err(1960): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
10-12 18:33:34.736: W/System.err(1960): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
10-12 18:33:34.736: W/System.err(1960): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
10-12 18:33:34.736: W/System.err(1960): at com.praktikum.androidcrestclient.RestClient.convertStreamToFile(RestClient.java:181)
10-12 18:33:34.736: W/System.err(1960): at com.praktikum.androidcrestclient.RestClient.executeRequest(RestClient.java:152)
10-12 18:33:34.736: W/System.err(1960): at com.praktikum.androidcrestclient.RestClient.Execute(RestClient.java:112)
10-12 18:33:34.736: W/System.err(1960): at com.praktikum.androidcrestclient.MainActivity.retrieveSampleData(MainActivity.java:37)
10-12 18:33:34.736: W/System.err(1960): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 18:33:34.736: W/System.err(1960): at java.lang.reflect.Method.invoke(Method.java:507)
10-12 18:33:34.736: W/System.err(1960): at android.view.View$1.onClick(View.java:2139)
10-12 18:33:34.736: W/System.err(1960): at android.view.View.performClick(View.java:2485)
10-12 18:33:34.746: W/System.err(1960): at android.view.View$PerformClick.run(View.java:9080)
10-12 18:33:34.746: W/System.err(1960): at android.os.Handler.handleCallback(Handler.java:587)
10-12 18:33:34.746: W/System.err(1960): at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 18:33:34.746: W/System.err(1960): at android.os.Looper.loop(Looper.java:123)
10-12 18:33:34.746: W/System.err(1960): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-12 18:33:34.746: W/System.err(1960): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 18:33:34.756: W/System.err(1960): at java.lang.reflect.Method.invoke(Method.java:507)
10-12 18:33:34.756: W/System.err(1960): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-12 18:33:34.756: W/System.err(1960): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-12 18:33:34.756: W/System.err(1960): at dalvik.system.NativeStart.main(Native Method)
10-12 18:33:34.766: W/System.err(1960): java.lang.NullPointerException
Please, can someone help!!
Thanks
回答1:
First open the path, then add the file:
Updated
String dir = Environment.getExternalStorageDirectory(); // getAbsolutePath is not requried
File path = new File(dir);
File root = new File(path, "ekg_daten.zip");
来源:https://stackoverflow.com/questions/12865267/java-io-filenotfoundexception-access-denied-even-though-ive-putted-permissions