2
我不能讓我的應用程序寫入的SD卡。我嘗試了幾種不同的方法,但他們都沒有工作。我在SO上看到了幾個帖子,這些帖子都有同樣的問題,並且問題沒有解決。有人可以看看我的代碼,看看有什麼不對嗎?failling寫入到SD卡
我有<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在我的清單。
String inFileName = "/data/data/cpe495.smartapp/files/test.csv"; //TODO Use folder/filename
File inFile = new File(inFileName);
try{
FileInputStream fis = new FileInputStream(inFile);
String outFileName = Environment.getExternalStorageDirectory()+"/test.csv"; //TODO Use output filename
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0)
output.write(buffer, 0, length);
output.flush();
output.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
04-06 00:17:14.961: WARN/System.err(279): java.io.FileNotFoundException: /mnt/sdcard/test.csv (Permission denied)
04-06 00:17:15.000: WARN/System.err(279): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
04-06 00:17:15.000: WARN/System.err(279): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
04-06 00:17:15.012: WARN/System.err(279): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
04-06 00:17:15.022: WARN/System.err(279): at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
04-06 00:17:15.032: WARN/System.err(279): at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
04-06 00:17:15.032: WARN/System.err(279): at cpe495.smartapp.SmartApp$8.onClick(SmartApp.java:165)
04-06 00:17:15.041: WARN/System.err(279): at android.view.View.performClick(View.java:2408)
04-06 00:17:15.051: WARN/System.err(279): at android.view.View$PerformClick.run(View.java:8816)
04-06 00:17:15.062: WARN/System.err(279): at android.os.Handler.handleCallback(Handler.java:587)
04-06 00:17:15.062: WARN/System.err(279): at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 00:17:15.072: WARN/System.err(279): at android.os.Looper.loop(Looper.java:123)
04-06 00:17:15.072: WARN/System.err(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-06 00:17:15.081: WARN/System.err(279): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 00:17:15.091: WARN/System.err(279): at java.lang.reflect.Method.invoke(Method.java:521)
04-06 00:17:15.091: WARN/System.err(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-06 00:17:15.101: WARN/System.err(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-06 00:17:15.101: WARN/System.err(279): at dalvik.system.NativeStart.main(Native Method)
你是一個真正的設備上或在仿真器上運行呢?如果它是仿真器,請確保設備具有設置的虛擬SD卡。 – Drew 2011-04-06 01:36:33
虛擬和真實,我如何設置虛擬SD卡? – prolink007 2011-04-06 02:14:42
@德魯:我設置SD卡後,它的工作。由於 – prolink007 2011-04-06 04:01:53