我已经找到了我上面提出的问题的解决方案。
显然有一种简单的方法可以让特定文件夹中的文件不显示在您的应用程序中......
我之前犯的错误是我曾经Environment.getExternalStorageDirectory();
将所有应用程序文件直接写入sd卡,它们出现在我的列表中。
我不得不使用Context.getExternalFilesDir("FolderName");
并且文件现在没有显示在列表中,原因是该文件夹现在是您的应用程序的本地文件夹,媒体无法访问。
请阅读 getExternalFilesDir 的完整 Java 文档以熟悉该类。
public File getExternalFilesDir
(字符串类型)
自:API 8 级
返回外部文件系统上目录的绝对路径(即 Environment.getExternalStorageDirectory() 上的某个位置),应用程序可以在其中放置它拥有的持久文件。这些文件对于应用程序是私有的,并且通常不作为媒体对用户可见。
这就像 getFilesDir() 一样,在卸载应用程序时这些文件将被删除,但是有一些重要的区别:
外部文件并不总是可用:如果用户在计算机上安装或移除外部存储,它们就会消失。有关存储状态的信息,请参阅环境 API。这些文件没有强制执行安全性。所有应用程序都可以读取和写入放置在此处的文件。以下是在应用程序的私有存储中操作文件的典型代码示例:
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(R.drawable.balloons);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
void deleteExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
if (file != null) {
file.delete();
}
}
boolean hasExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
if (file != null) {
return file.exists();
}
return false;
}
如果您为此函数提供非空类型,则返回的文件将是给定类型的子目录的路径。尽管媒体扫描仪不会自动扫描这些文件,但您可以使用MediaScannerConnection.scanFile
. 请注意,这与Environment.getExternalStoragePublicDirectory()
提供所有应用程序共享的媒体目录的 不同。此处返回的目录归应用所有,卸载应用时会删除其内容。与 Environment.getExternalStoragePublicDirectory() 不同,此处返回的目录将自动为您创建。
下面是一个典型代码示例,用于操作应用程序私有存储中的图片并将其添加到媒体数据库:
void createExternalStoragePrivatePicture() {
// Create a path where we will place our picture in our own private
// pictures directory. Note that we don't really need to place a
// picture in DIRECTORY_PICTURES, since the media scanner will see
// all media in these directories; this may be useful with other
// media types such as DIRECTORY_MUSIC however to help it classify
// your media for display to the user.
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
try {
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(R.drawable.balloons);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
void deleteExternalStoragePrivatePicture() {
// Create a path where we will place our picture in the user's
// public pictures directory and delete the file. If external
// storage is not currently mounted this will fail.
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (path != null) {
File file = new File(path, "DemoPicture.jpg");
file.delete();
}
}
boolean hasExternalStoragePrivatePicture() {
// Create a path where we will place our picture in the user's
// public pictures directory and check if the file exists. If
// external storage is not currently mounted this will think the
// picture doesn't exist.
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (path != null) {
File file = new File(path, "DemoPicture.jpg");
return file.exists();
}
return false;
}
参数 type 要返回的文件目录的类型。对于文件目录的根目录或子目录的以下环境常量之一可能为 null:DIRECTORY_MUSIC、DIRECTORY_PODCASTS、DIRECTORY_RINGTONES、DIRECTORY_ALARMS、DIRECTORY_NOTIFICATIONS、DIRECTORY_PICTURES 或 DIRECTORY_MOVIES。
返回 返回外部存储上保存应用程序文件的目录的路径。如果当前未安装外部存储,则返回 null,因此无法确保路径存在;当它可用时,您将需要再次调用此方法。