2011-10-11 54 views
4

与文件存储最接近的文档是this post(如果无法访问,请参阅下面的内容),但这会给我留下几个问题。在角落上访问存储

我真的真的很喜欢关于什么路径映射到这里的存储空间的明智解释,看到我们应该如何对它们进行硬编码,以及我们期望如何精确访问它们。实际的代码示例将是最高级的。我最好的猜测是:

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

如何访问外部存储设备(可选,另外可以弹出的设备)sdcard,如果/sdcard maps to restricted storage instead

我们引用的Nook开发者文档:

背景有对市场的今天,一个可用来在/ data分区 应用程序和一个角落 颜色设备只有250MB两种不同的分区方案4GB可用于/ data分区上的 应用程序。因此,应用程序的设计和开发势在必行 有效地管理空间。未能这样做的应用程序将不会被 通过商店接受分发。

领域相关技术建议或解决方案,如果你的 应用程序需要大量数据(包括但不限 于图片,音频或视频内容),你应该在运行时下载这些 资源,并将它们存储在更大的分区 设备。如果您的应用程序将请求和存储超过 100MB的数据或资源必须由以下 限制遵守:

您的应用程序中提供了大量的数据是描述 必须清楚和明确规定由 应用程序使用/交付。您必须将您的资源和数据写入适当的 分区。您可以检测,如果该设备具有放大/数据 分区如下:

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 ... 
} 

在此书写/数据

数据到应用程序的私人空间
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE); 

你的应用程序不应该承担 存在设备上的SD卡,但可以通过呼叫测试一个 至

Environment.getExternalStorageState();如果未找到SD卡, 您的应用程序必须正常退出,并通知用户 关于退出的原因。

记住,访问/媒体分区,以及 ExternalStorage你需要在你的清单声明:

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

这似乎是不可能访问您提供的链接:http://nookdeveloper.zendesk.com/entries/20257971-updated-what-are-the-size -constraints-on-my-applications没有注册(这看起来不像一个单击过程)。你应该在某个地方复制讨论,这样更多的人可以访问它。 – Idolon

+0

说文字被复制。 – Turnsole

回答

0

首先请尝试以下方法:

如果没有那些回报你的目录,你可以写,检查以下google-groups thread,并使用在最后回答中提供的代码,其中列举了目前所有的安装点:

我与Galaxy S有同样的问题。到目前为止,所有Android设备 都得到了“安装”命令。我构建了一个解析“安装”响应的可用卷 的列表。这不是完美的,但切肉刀比Android 存储API。

String cmd = "/system/bin/mount"; 
try { 
    Runtime rt = Runtime.getRuntime(); 
    Process ps = rt.exec(cmd); 

    BufferedReader rd = new BufferedReader(new InputStreamReader(ps.getInputStream())); 
    String rs; 
    while ((rs = rd.readLine()) != null) 
    { 
     //check what you need! 
     Log.i("MOUNT_CMD", rs); 
    } 
    rd.close(); 
    ps.waitFor(); 
} catch(Exception 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); 
+0

@Hydrangea你尝试过吗?我想知道我提到的一种解决方案是否适合你。还有一个想法:有些设备将内置的SD卡安装到'/ sdcard/sd /'路径上,您也可以在Nook上尝试它。 – Idolon

+0

即将坐下来。我对这种缓慢的反应表示歉意。 – Turnsole

+0

所以,看起来我没有时间回到今晚,但不想浪费赏金,它很快就会到期,所以它是你的。尽快回到问题。 – Turnsole

4

好了,这就是我所学到在过去几周。

如果要写入内部 SD卡,使用Context.getFilesDir()。它会返回您的应用程序的私人目录。您无法在内部闪存(又名“/ data”)上创建自己的目录。除了应用程序分配的文件夹外,您无权写入任何地方。假设有两个内部分区,“/ data”和“/ media”,但我无法获得“/ media”来拯救我的生命。

如果存在闪存“/ sdcard”,则可以使用外部闪存。这是您可以从设备中弹出的卡。有两种方法去了解这一点:分配给您的应用程序文件夹中

  • 店里的东西(所以它会被删除 当你的应用被卸载)。您可以使用 Context.getExternalFilesDir()找到该文件夹​​。
  • 将东西存储在“/ sdcard/foo/bar”下的某个硬编码路径中,或存储在Environment.getExternalStorageDirectory()/whatever中的任何位置。

This post,由B &Ñ代表(我在我的问题提及)原来是有点红鲱鱼,“/ SD卡”不映射到的eMMC槽,并且我不知道“我们将SD卡映射到我们的内部eMMC”是什么意思。

This B&N post说,“/ media”是内部的,但即使我拥有适当的清单权限,我也无法写入它...所以请进入图。

这是我的测试设备的撷取画面,显示是什么,是不是访问: enter image description here

操作的代码(注意,文件实用程序未包括在默认情况下,SDK,它是从组织.apache.commons.io LIB):

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"; 
}