与文件存储有关的最接近文档的是这个职位 (如果您无法访问它,请参见下面),但它给我留下了几个问题。
我真的非常想要一个有知识的解释什么路径映射到什么存储,看到我们应该如何对它们进行硬编码,以及我们应该如何精确地访问它们。一个实际的代码示例将是最高级的。我对这件事的猜测是:
如果是/sdcard将映射到受限存储,我们如何访问外部(可选的、附加的、可以弹出的) sdcard?
现在引用Nook developer文档:
背景当今市面上的NOOK设备有两种不同的分区方案,一种只有250 4GB可供/data分区上的应用程序使用,另一种为/data分区上的应用程序可用4GB。因此,应用程序的设计和开发必须能够有效地管理空间。没有这样做的申请将不被接受通过商店分发。 如果应用程序需要大量数据(包括但不限于图像、音频或视频内容),则应在运行时下载这些资源并将其存储在设备的较大分区中。如果应用程序要请求和存储超过100 If的数据或资源,则必须遵守以下限制: 如果应用程序使用/交付了大量数据,则应用程序必须在说明中明确和显式地声明。必须将资源和数据写入适当的分区。您可以检测设备是否有扩大的/data分区,如下所示: stat =新StatFs("/data");long bytesAvailable = ( long )stat.getBlockSize() *(long)stat.getBlockCount();long megAvailable = bytesAvailable / 1048576;if (megAvailable > 1000){ .用/data }或{.将资源写入/mnt/media .} 在/data上将数据写入应用程序的私有空间 FileOutputStream fos =openFileOutput(文件名,Context.MODE_WORLD_READABLE); 您的应用程序不应该假设设备上存在sdcard,但是可以通过调用 如果找不到SD卡,应用程序必须优雅地退出,并通知用户退出的原因。 请记住,要访问/media分区以及您需要在清单中声明的ExternalStorage:
发布于 2011-10-16 15:33:14
首先,尝试以下方法:
如果它们都没有返回您可以写入的目录,请检查以下线程,并使用最后一个答案中提供的代码,该代码枚举所有当前挂载点:
我对Galaxy也有同样的问题,直到现在我所有的Android设备 有“挂载”命令可用。我构建了一个可用的卷解析“挂载”响应列表。这不是完美的,但比Android存储API更聪明。 字符串cmd =“/system/bin/挂载”;尝试{ Runtime = Runtime.getRuntime();Process ps = rt.exec(cmd);BufferedReader rd =新BufferedReader(新的InputStreamReader(ps.getInputStream( );String rs;while ((rs = rd.readLine()) != null) {//检查您需要的!Log.i("MOUNT_CMD",rs);} rd.close();ps.waitFor();}catch(异常e) {//.}
如果可以在Nook设备中插入microSD卡,则还可以尝试以下操作:
mVolumeManagerReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.i("MediaMounter", "Storage: " + intent.getData());
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");
context.registerReceiver(mVolumeManagerReceiver, filter);
发布于 2011-10-22 22:39:30
好吧,这是我在过去几周里学到的。
如果您想要写入内部 SDcard,请使用它将返回应用程序的私有目录。您不能在内部闪存(又名"/data")上创建自己的目录。除了分配给应用程序的文件夹之外,您没有编写其他地方的权限。据说有两个内部分区,"/data“和"/media",但我不能使用"/media”来挽救我的生命。
当可用时,您可以使用外部闪存"/sdcard“。这是你可以从设备中弹出的卡片。要做到这一点有两种方法:
一位B&N的代表(我在我的问题中提到了这一点)的这个职位被证明有点像鲱鱼,"/sdcard“并不能映射到eMMC插槽,我也不知道”我们把SD卡映射到我们的内部eMMC“意味着什么。
这个B&N员额说"/media“是内部的,但我不能写信给它,即使我有适当的清单权限.那就去想想吧。
这是我的测试设备的一个尖叫,显示了什么是可访问的,哪些是不可访问的:
这方面的代码(请注意,FileUtils默认不包括在sdk中,它来自org.apache.commons.io库):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView dataView = (TextView) findViewById(R.id.data);
dataView.setText(testIt("/data"));
TextView mediaView = (TextView) findViewById(R.id.media);
mediaView.setText(testIt("/media"));
TextView mntMediaView = (TextView) findViewById(R.id.mntMedia);
mntMediaView.setText(testIt("/mnt/media"));
try {
File fd = this.getFilesDir();
if(fd != null) {
TextView fdView = (TextView) findViewById(R.id.filesDir);
fdView.setText("getFilesDir(): " + testIt(fd.toString()));
}
} catch (Exception e) {
e.printStackTrace();
}
try {
File efd = this.getExternalFilesDir(null);
if(efd != null) {
TextView efdView = (TextView) findViewById(R.id.externalFilesDir);
efdView.setText("getExternalFilesDir(): " + testIt(efd.toString()));
}
} catch (Exception e) {
e.printStackTrace();
}
try {
File esd = Environment.getExternalStorageDirectory();
if(esd != null) {
TextView esdView = (TextView) findViewById(R.id.externalStorageDirectory);
esdView.setText("getExternalStorageDirectory(): " + testIt(esd.toString()));
}
} catch (Exception e) {
e.printStackTrace();
}
try {
File espd = Environment.getExternalStoragePublicDirectory(null);
if(espd != null) {
TextView espdView = (TextView) findViewById(R.id.externalStoragePublicDirectory);
espdView.setText("getExternalStoragePublicDirectory(): " + testIt(espd.toString()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String testIt(String dir){
StatFs stat = new StatFs(dir);
long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getBlockCount();
long megAvailable = bytesAvailable / FileUtils.ONE_MB;
File dirFile = new File(dir + "/test/");
dirFile.mkdir();
return dir + "/test \n canRead() " + dirFile.canRead() + ", \n canWrite() " + dirFile.canWrite() + " with " + megAvailable + "MB available";
}
https://stackoverflow.com/questions/7720765
复制相似问题