如何为我的 Android 应用程序创建一个文件夹,该文件夹将在 DCIM、下载、铃声、图片等的同一级别可见
How do I create a folder for my Android app which will be visible at the same level of DCIM, Downloads, Ringtones, Pictures etc
我想为我的应用程序创建一个文件夹(当然是通过代码),它将在与某些系统文件夹相同的级别可见,例如DCIM、下载、铃声、图片都是。我没有安装 SD 卡。
我尝试了以下选项,但其中 none 正在所需位置创建文件夹,并且当我将 phone 连接到我的笔记本电脑时,其中 none 是可见的USB.
尝试 1
File appDir = new File(context.getCacheDir(), "ABC");
尝试 2
File appDir = new File(context.getFilesDir(), "ABC");
尝试 3
File appDir = new File(android.os.Environment.getExternalStorageDirectory(), "ABC");
尝试 4
File appDir = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM), "../ABC");
有人可以帮忙吗?
编辑:
根据 CommonsWare 的回答,以下代码已创建并在正确位置显示文件夹。
File appDir = new File(android.os.Environment.getExternalStorageDirectory(), "ABC");
FileOutputStream fos = new FileOutputStream(new File(appDir, "test"));
byte buffer[] = new byte[100];
fos.write(buffer, 0, 100);
fos.close();
String paths[] = {new File(appDir, "test").getAbsolutePath()};
String mimeTypes[] = {null};
MediaScannerConnection.scanFile(context, paths, mimeTypes, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
// Nothing to do
}
});
none of them are creating the folder at the desired location
"Attempt 3"是。
none of them are visible when I connect my phone to my laptop through USB
前两个将不可见,因为它们在 internal storage 上,无法在生产硬件上浏览。
后两者将不可见,直到您将文件写入目录,然后使用 MediaScannerConnection
及其 scanFile()
方法让 MediaStore
将文件添加到索引.即使在此之后,您可能仍需要在笔记本的文件管理器中执行 "refresh" 操作才能看到新文件(以及保存它的目录)。
我想为我的应用程序创建一个文件夹(当然是通过代码),它将在与某些系统文件夹相同的级别可见,例如DCIM、下载、铃声、图片都是。我没有安装 SD 卡。
我尝试了以下选项,但其中 none 正在所需位置创建文件夹,并且当我将 phone 连接到我的笔记本电脑时,其中 none 是可见的USB.
尝试 1
File appDir = new File(context.getCacheDir(), "ABC");
尝试 2
File appDir = new File(context.getFilesDir(), "ABC");
尝试 3
File appDir = new File(android.os.Environment.getExternalStorageDirectory(), "ABC");
尝试 4
File appDir = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM), "../ABC");
有人可以帮忙吗?
编辑: 根据 CommonsWare 的回答,以下代码已创建并在正确位置显示文件夹。
File appDir = new File(android.os.Environment.getExternalStorageDirectory(), "ABC");
FileOutputStream fos = new FileOutputStream(new File(appDir, "test"));
byte buffer[] = new byte[100];
fos.write(buffer, 0, 100);
fos.close();
String paths[] = {new File(appDir, "test").getAbsolutePath()};
String mimeTypes[] = {null};
MediaScannerConnection.scanFile(context, paths, mimeTypes, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
// Nothing to do
}
});
none of them are creating the folder at the desired location
"Attempt 3"是。
none of them are visible when I connect my phone to my laptop through USB
前两个将不可见,因为它们在 internal storage 上,无法在生产硬件上浏览。
后两者将不可见,直到您将文件写入目录,然后使用 MediaScannerConnection
及其 scanFile()
方法让 MediaStore
将文件添加到索引.即使在此之后,您可能仍需要在笔记本的文件管理器中执行 "refresh" 操作才能看到新文件(以及保存它的目录)。