use*_*558 1 media directory android
ey up.我建立了一个简单的音乐应用程序,从SD卡读取wav文件并播放它们.我如何访问默认媒体目录?这就是我获得SD卡的方式
public void LoadSounds() throws IOException
{
String extState = Environment.getExternalStorageState();
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
File sd = new File(Environment.getExternalStorageDirectory ()); //this needs to be a folder the user can access, like media
Run Code Online (Sandbox Code Playgroud)
像往常一样,文档没有提供实际的使用示例,但它说明了这一点 - 如果您使用的是API级别8或更高级别,请使用getExternalFilesDir()打开一个文件,该文件代表应保存文件的外部存储目录.此方法采用type参数指定所需的子目录类型,例如DIRECTORY_MUSIC ...
我该如何使用它?谢谢
编辑:如果我尝试用文件路径字符串填充微调器数组,这会使它崩溃.
File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
File sd = new File(path, "/myFolder");
File[] sdDirList = sd.listFiles(new WavFilter());
if (sdDirList != null)
{
//sort the spinner
amountofiles = sdDirList.length;
array_spinner=new String[amountofiles];
......
final Spinner s = (Spinner) findViewById(R.id.spinner1); //crashes here
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,
android.R.layout.select_dialog_item, array_spinner);
Run Code Online (Sandbox Code Playgroud)
EDIT2:好的,所以我做了这个测试,应该写一个txt文件到音乐目录.我运行应用程序,没有txt文件写在我能找到的设备上的任何地方.
// Path to write files to
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
String fname = "mytest.txt";
// Current state of the external media
String extState = Environment.getExternalStorageState();
// External media can be written onto
if (extState.equals(Environment.MEDIA_MOUNTED))
{
try {
// Make sure the path exists
boolean exists = (new File(path)).exists();
if (!exists){ new File(path).mkdirs(); }
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + fname);
fOut.write("Test".getBytes());
// Close output stream
fOut.flush();
fOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
另一个编辑:我会得到这个工作!因此,如果我使用此行,它会在SD卡上创建一个名为"Musictest"的文件夹.不懂?
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "test").getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)
////////////////////////////////////////////////// //////////////////最终编辑:对,所以这将在设备音乐目录中查找名为test的文件夹.如果它不存在,它将被创建.(这里有一些修复,如果为空则出错)然后列出目录中的文件并将它们添加到数组中.
public void LoadSounds() throws IOException
{
String extState = Environment.getExternalStorageState();
// Path to write files to
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/test").getAbsolutePath();
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
//do your file work here
// Make sure the path exists
boolean exists = (new File(path)).exists();
//if not create it
if (!exists){ new File(path).mkdirs(); }
File sd = new File(path);
//This will return an array with all the Files (directories and files)
//in the external storage folder
File[] sdDirList = sd.listFiles();
if (sdDirList != null)
{
//add the files to the spinner array
array_spinnerLoad=new String[sdDirList.length];
files = new String[sdDirList.length];
for(int i=0;i<sdDirList.length;i++){
array_spinnerLoad[i] = sdDirList[i].getName();
files[i] = sdDirList[i].getAbsolutePath();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ari*_*ayu 11
如文档中所述,getExternalFilesDir()返回File.File对象可以表示文件或目录.
因此:
File musicDirectory = new File( getExternalFilesDir(Environment.DIRECTORY_MUSIC));
Run Code Online (Sandbox Code Playgroud)
会给你玩的对象.
归档时间: |
|
查看次数: |
14116 次 |
最近记录: |