我可以下载文件,而无需使用手机内存,而不是虚拟SD卡模拟器? [英] Can i download file without using the phone memory instead of virtual SD card in emulator?

查看:282
本文介绍了我可以下载文件,而无需使用手机内存,而不是虚拟SD卡模拟器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在参考质疑

<一个href=\"http://stackoverflow.com/questions/2147704/can-anyone-explain-the-file-parameters-used-to-download-file-in-android\">http://stackoverflow.com/questions/2147704/can-anyone-explain-the-file-parameters-used-to-download-file-in-android

我可以不用创建虚拟SD卡。有什么办法来保存内存在手机内存中的文件?如果有可能不使用虚拟SD卡,那么如何?

Can I do it without creating virtual SD Card. Is there any way to save the file in phone memory in internal memory? if it is possible without using the virtual SD Card, then how?

推荐答案

你的问题是有点不清楚,你想用的,所以这里的两个。

your question is a bit unclear about which you want to use, so here's both.

像mbaird说,你可以轻松地使用文件保存到手机的内部存储
Context.openFileOutput()。例如:

Like mbaird said, you can easily save a file to the phone's internal storage using Context.openFileOutput(). For example:

// Create file on internal storage and open it for writing
FileOutputStream fileOut;
try {
    fileOut = openFileOutput(userId +".ics", Context.MODE_PRIVATE);
} catch (IOException e) {
    // Error handling
}

// Write to output stream as usual
// ...

这将创造对手机的内部存储新的文件,在这样的路径:结果
/data/data/com.example.yourpackagename/files/123456.ics

This would create a new file on the phone's internal storage, at a path like this:
/data/data/com.example.yourpackagename/files/123456.ics.

只有你的应用程序可以读取该文件;其他人将无法读取该文件,就像他们,如果它是在SD卡上。

Only your application can read this file; others will not be able to read this file, like they would if it was on the SD card.

如果您想将文件保存到SD卡上,你需要的东西是这样的:

If you want to save a file to the SD card, you need something like this:

if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
    // SD card is not available
    return;
}

File root = Environment.getExternalStorageDirectory() +"/myapp/";
File newFile = new File(root, userId +".ics");
FileOutputStream fileOut = new FileOutputStream(newFile);

// Write to output stream as usual
// ...

正如你所看到的,你不能依靠SD卡的存在present和可在任意时间给定的点写。这可能是以下几个原因:

As you can see, you cannot rely on an SD card being present and available for writing to at any given point in time. This could be for several reasons:


  • 的设备/仿真器没有SD卡

  • SD卡正在与PC
  • 共享
  • SD卡为只读

  • SD卡无文件系统

  • SD卡已损坏

这篇关于我可以下载文件,而无需使用手机内存,而不是虚拟SD卡模拟器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆