gpt4 book ai didi

android - 在角落访问存储

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:46 24 4
gpt4 key购买 nike

我能找到的与文件存储有关的最接近文档的是 this post (如果您无法访问它,请参阅下文),但它给我留下了几个问题。

我真的、真的、真的想要一个关于什么路径映射到什么存储的知识渊博的解释,看看我们应该如何硬编码它们,以及我们期望访问它们的精确程度。实际的代码示例将是最高级的。我最好的猜测是:

  • /sdcard->映射到内部eMMC插槽,访问受限。环境.getExternalStorageDirectory(); ...仍然返回这个。
  • /media -> 映射到内部 8GB 内存(我可以写入)
  • /数据 -> ?
  • >? -> 映射到可选的 microSD 卡

我们如何访问外部(可选的,附加的,你可以弹出的那个)sdcard,如果/sdcard maps to restricted storage instead

现在引用 Nook 开发者文档:

Background There are two different partition schemes for the NOOK Color devices in market today, one with only 250MB available to applications on the /data partition and one with 4GB available to applications on the /data partition. As a result, it is imperative that applications are designed and developed in such a way as to manage space effectively. Applications which fail to do so will not be accepted for distribution via the Shop.

Area Associated Technical Recommendation or Solution if your application requires large amount of data (including but not limited to images, audio or video content), you should download those resources at runtime and store them in the larger partition of the device. If your application is going to request and store more than 100MB of data or resource you MUST abide by the the following restrictions:

Your application must clearly and explicitly state in the description provided that a large amount of data is used/delivered by the application. You MUST write your resources and data onto appropriate partition. You can detect if the device has an enlarged /data partition as follows :

StatFs stat = new StatFs("/data"); 
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
if (megAvailable > 1000){
... write your resources in /data
} else {
... write your resources on /mnt/media ...
}

To write data into your application's private space on /data

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE); 

Your application should NOT assume the presence of an sdcard on device, but you can test for one via a call to

Environment.getExternalStorageState(); If an SD Card is not found, your application MUST exit gracefully with a notification to the user as to the reason for the exit.

Remember, that to access the /media partition, as well as ExternalStorage you need to declare in your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>

最佳答案

好的,这是我在过去几周学到的东西。

如果您想写入内部 SD 卡,请使用Context.getFilesDir()。它将返回您的应用程序的私有(private)目录。您不能在内部闪存(又名“/data”)上创建自己的目录。除了您的应用程序分配的文件夹之外,您无权写入任何地方。据说有两个内部分区,“/data”和“/media”,但我无法进入“/media”以挽救我的生命。

您可以使用外部 闪存“/sdcard”(如果可用)。这是您可以从设备中弹出的卡片。有两种方法可以解决这个问题:

  • 将内容存储在分配给您的应用的文件夹中(这样它就会被删除当您的应用程序被卸载时)。你可以找到那个文件夹Context.getExternalFilesDir()
  • 将东西存储在任何地方,要么在“/sdcard/foo/bar”下的一些硬编码路径中,要么在Environment.getExternalStorageDirectory()/其他。

This post由 B&N 代表(我在我的问题中引用)结果有点转移注意力,“/sdcard”没有映射到 eMMC 插槽,我不知道“我们将 SD 卡映射到我们的内部 eMMC” 的意思。

This B&N post说“/media”是内部的,但即使我有适当的 list 权限,我也不能写信给它……所以去想想吧。

这是我的测试设备的屏幕截图,显示了哪些是可访问的,哪些是不可访问的: enter image description here

相关代码(注意 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";
}

关于android - 在角落访问存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com