jas*_*mko 25 android android-file
我需要res/raw /中所有文件的名称(String)
我试过了:
File f = new File("/");
String[] someFiles = f.list();
Run Code Online (Sandbox Code Playgroud)
它看起来像根目录是android模拟器的根...而不是我的计算机根目录.这有足够的意义,但并没有真正帮助我找出原始文件夹的存在位置.
更新:感谢所有的好评.似乎其中一些是有效的,但只让我走了一半.也许更详细的描述会有所帮助
我想获取原始文件夹中的所有mp3文件,以便我可以获取所有名称,然后将它们添加到URI以便以下列方式播放随机MP3 ...
String uriStr = "android.resource://"+ "com.example.phone"+ "/" + "raw/dennis";
Uri uri = Uri.parse(uriStr);
singletonMediaPlayer = MediaPlayer.create(c, uri);
Run Code Online (Sandbox Code Playgroud)
当我将"dennis.mp3"放入assets文件夹时,它会按预期显示,但是,使用上面的代码,我再也无法访问该MP3,除非有以下内容:
String uriStr = "android.assets://"+ "com.example.phone"+ "/" + "dennis";
Uri uri = Uri.parse(uriStr);
Run Code Online (Sandbox Code Playgroud)
Cap*_*mmo 61
要列出原始资产的所有名称(基本上是剥离了扩展名的文件名),您可以执行以下操作:
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Run Code Online (Sandbox Code Playgroud)
由于实际文件不仅仅是在文件系统上,而是在手机上,因此名称无关紧要,您需要通过分配给该资源名称的整数来引用它们.在上面的例子中,你可以得到这个整数:
int resourceID=fields[count].getInt(fields[count]);
Run Code Online (Sandbox Code Playgroud)
这与您通过引用R.raw.whateveryounamedtheresource获得的int相同
此代码将从sdCard的"New Foder"中检索所有文件.
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "New Folder");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
Log.i("file names", name);
}
}
Run Code Online (Sandbox Code Playgroud)
并确保在manifest.xml文件中添加android sd卡写入权限
| 归档时间: |
|
| 查看次数: |
48478 次 |
| 最近记录: |