Android的访问外部存储 [英] Android Access External Storage

查看:93
本文介绍了Android的访问外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的一些存储问题。我工作的一个应用程序,我需要访问手机的船上外部存储器和外部SD卡存储这我得到这个code

I am facing some storage issue. i am working on an application where i have to access phone's on board external storage and an External sdCard storage which i am getting with this code

Environment.getExternalStorageDirectory();

但是,当板载外部存储器存在,那么这个code返回不是外部SD卡的板载内存的路径,而SD卡安装

But when onBoard external memory exists then this code returns the path of onBoard memory not the external sdCard while the sdCard is mounted

现在的问题是.. 我想板载访问两个存储器和外部SD卡有没有什么办法来访问内存

now the problem is.. i want to access both memories onBoard and external sdcard is there any way to access both memories

推荐答案

您可以在设备的内部存储直接将文件保存。默认情况下,保存在内部存储的文件是私有的,你的应用程序和其他应用程序不能访问这些文件(也不能在用户)。 当用户卸载应用程序,这些文件将被删除。

Using the Internal Storage

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

要创建并写入一个私人文件到内部存储:

To create and write a private file to the internal storage:

呼叫openFileOutput()与该文件的名称和操作模式。这将返回一个FileOutputStream。 写与写文件()。 紧密关闭的流()。 例如:

Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream. Write to the file with write(). Close the stream with close(). For example:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

使用外部存储

检查介质的可用性

Using the External Storage

Checking media availability

插入许可清单中第一个

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

在你的外部存储任何工作,你应该总是调用getExternalStorageState()检查媒体是否可用。介质可能被安装到计算机,缺失,只读,或在一些其它状态。例如,这里是你如何检查的有效性:

Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state. For example, here's how you can check the availability:

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

  if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
  } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
   mExternalStorageAvailable = true;
   mExternalStorageWriteable = false;
  } else {
// Something else is wrong. It may be one of many other states, but all we need
//  to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

使用这些你可以得到绝对路径,外部存储

 String m_str = Environment.getExternalStorageDirectory() .getAbsolutePath().toString();

外部存储访问文件

如果您使用的是API级别8或更高,使用getExternalFilesDir()打开重新presents外部存储目录,你应该保存文件的文件。这个方法有一个类型参数,指定你想要的子目录,如DIRECTORY_MUSIC和DIRECTORY_RINGTONES类型(传递null收到您的应用程序的文件目录的根目录)。如有必要,此方法将创建相应的目录。通过指定目录的类型,可以确保在Android的媒体扫描仪将正常文件分类系统(例如,铃声被确定为手机铃声,而不是音乐)。如果用户卸载应用程序,这个目录及其所有内容将被删除。

Accessing files on external storage

If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. By specifying the type of directory, you ensure that the Android's media scanner will properly categorize your files in the system (for example, ringtones are identified as ringtones and not music). If the user uninstalls your application, this directory and all its contents will be deleted.

如果您正在使用API​​ 7级或更低,使用getExternalStorageDirectory(),打开一个文件重新presenting外部存储的根目录

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage

 File f = new File(Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + File.separator + fileName);

   Or
 final File file = new File(Environment.getExternalStorageDirectory()
                      .getAbsolutePath(), filename);

这篇关于Android的访问外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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