Android 外部写入允许但文件未找到异常
在模拟器中,我尝试写入文件:
/mnt/sdcard/Android/data/com.Me.MyApp/files/myFile.txt
我已在清单中设置外部写入权限,但我不断收到文件未找到的异常。模拟器配置有 SD 卡。
为什么会这样?
InputStream is = c.getResources().openRawResource(R.raw.my_raw_file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
try
{
while (zis.getNextEntry() != null)
{
File destFile = new File(destinationPath);
// EXCEPTION THROWN NEXT LINE!
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[BUF_SIZE];
int count;
while ((count = zis.read(buffer)) != -1)
{
os.write(buffer, 0, count);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
zis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
In the emulator I'm trying to write to the file:
/mnt/sdcard/Android/data/com.Me.MyApp/files/myFile.txt
I have set external write permissions in the Manifest, but I keep receiving a file not found exception. The emulator is configured to have an sd-card.
Why might that be?
InputStream is = c.getResources().openRawResource(R.raw.my_raw_file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
try
{
while (zis.getNextEntry() != null)
{
File destFile = new File(destinationPath);
// EXCEPTION THROWN NEXT LINE!
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[BUF_SIZE];
int count;
while ((count = zis.read(buffer)) != -1)
{
os.write(buffer, 0, count);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
zis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试下面的代码
Try below code
Nammari 的答案加上:
Android 教程:在模拟器中创建和使用 SD 卡
http://www.streamhead.com/android-tutorial-sd-card/
Nammari's answer plus:
Android Tutorial: Creating and Using an SD Card in the Emulator
http://www.streamhead.com/android-tutorial-sd-card/