无法写入 SD 卡
我无法让我的应用程序写入 SD 卡。我尝试了几种不同的方法,但没有一个有效。我在这里看到了几篇关于 SO 的帖子,它们也有同样的问题,但问题尚未解决。有人可以看一下我的代码并看看有什么问题吗?
我的清单中有
。
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)
I can not get my application to write to the sdcard. I have tried several different ways and none of them are working. I have seen several posts here on SO that are having the same problem and the problem is not resolved. Can someone please take a look at my code and see what is wrong?
I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in my manifest.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题的办法是给模拟器添加一张sd卡。将 SD 卡添加到模拟器后一切正常。
这是一种方法。
另一种方法是在 Eclipse 中调出设备列表,然后创建一个新的虚拟设备。在虚拟设备的设置过程中,会出现一个带有选项的窗口,询问您是否需要 SD 卡以及所需的大小。我就是这样做的,网上找不到教程。我在四处寻找选择时偶然发现了这一点。
提前致谢!
The solution to this problem was to add an sdcard to the emulator. After adding the sd card to the emulator everything worked fine.
Here is one way to do this.
The other way, is to pull up your list of devices in eclipse, then create a new virtual device. During the setup for the virtual device a window with options should come up that asks if you want an sdcard and what size you want it. This is how i did it, and i was unable to find a tutorial for it online. I stumbled on this on accident when looking around for options.
Thanks in advance!