安卓资源文件 [英] Android: resources file

查看:164
本文介绍了安卓资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了年龄试图找出如何让资源文件的目录( RES /原料)好几个月了......我只是找不到如何做到这一点...

I have spent ages trying to find out how to get the directory of the resources file (res/raw) for months now... I just can't find out how to do it...

我试图让资源的目录/生,这样我可以使用 listFiles()来使含在原始文件夹中的所有文件的数组,这样我可能会得到文件名称和排序,我将使用哪些文件或不通过有名称(如我可以再通过 R.raw.concat访问它们(fileIwantToAccess)),但我需要能够读取该文件夹中的所有文件,这样我可以决定,我用

I'm attempting to get the directory of res/raw so that I can use listFiles() to make an array containing all the files in the raw folder so that I may get the file names and sort which files I will use or not through there names (as I can then access them through R.raw.concat(fileIwantToAccess)) but I need to be able to read all the files in the folder so that I can decide which I use

是否有更简单的方法告诉Android的,我想这个文件夹的内容?

Is there an easier way to tell Android, that I would like the contents of this folder?

顺便说一句,我已经尝试使用

btw, Ive tried using

String directoryName = Soundboard.this.getFilesDir().getParent();

但它返回的路径: /data/data/com.soundboard
其中有两个子目录:LIB和文件,如果我再CONCAT两个目录,以目录名之前我做,当我使用 listFiles()返回任何文件; 但不为null,则返回 []

but it returns the path: /data/data/com.soundboard which has the two sub directories: "lib" and "files", if I then concat both directories to directoryName before I make the File it returns nothing when I use listFiles(); but not null, it returns []

请帮忙......我彻底失败了。

Please help... I am hopelessly lost

推荐答案

@迈克尔,

因为我们是太新,我们不能在对方的帖子发表评论。欢迎您,搭乘。

We cannot comment on each other’s posts because we are too new. Welcome, aboard.

要回答你的问题,你可以动态查询的音频文件可用。你就不能使用 / RES /原料目录这样做。您必须使用 /资产目录这一点。

To answer your question, you CAN dynamically query which audio files are available. You just can’t do so by using the /res/raw directory. You must use the /assets directory for this.

首先,我想澄清的是, / RES /原料 /资产都是合法的地方进行打包与应用程序的文件。它们被用于不同的目的,但是,每一个具有不同的意义。那么,什么是每个位置的意义,当(为什么)你应该把每个位置的文件?

First, I would like to clarify that /res/raw and /assets are both legitimate places to package files with an application. They are used for different purposes, however, and each has a different significance. So, what is the significance of each location, and when (or why) should you place files in each location?

好吧,让我们考虑 / RES /原料位置第一。在放置一个文件/ RES /原料使得文件中的资源的。有了Android,的资源的手段非常具体的东西;这并不仅仅意味着可以使用的东西。相反,一个的资源的是什么,可能有的热插拔的在运行时的多个版本的是。 活动的版本将取决于应用程序的上下文(环境)。例如,若干不同版本的相同的资源的可能存在于不同的区域和语言,对于不同的形状因子(屏幕尺寸),用于设备的不同取向,或甚至为应用程序的不同视觉主题!

Well, let's consider the /res/raw location first. Placing a file in /res/raw makes the file a Resource. With Android, Resource means something very specific; it doesn't just mean "something available to use". Rather, a Resource is something that could potentially have multiple versions that are "swappable" at runtime. The "active" version will depend on the application’s context (environment). For example, several different versions of the same "Resource" might exist for different locales and languages, for different form factors (screen sizes), for different orientations of the device, or even for different visual themes of the application!

资源通过资源的ID,这是类似于指针访问;它们指的是不同的变型是间接的。例如,我可以包3不同版本的图标与我的应用,每个适合不同的屏幕密度。在我的源$ C ​​$ C,虽然,我不是指单独每个版本;我只是有一个资源ID - 类似于 R.drawable.icon - 这指的是图标。当该器件是一款高密度的屏幕上运行, R.drawable.icon 点,以图标的喜密度版本;和低密度设备上, R.drawable.icon 点,以低密度版本。

Resources are accessed through Resource IDs, which are analogous to pointers; they refer to the different variants indirectly. For example, I might package 3 different versions of an icon with my application, each appropriate for a different screen density. In my source code, though, I don’t refer to each version individually; I simply have one resource ID - something like R.drawable.icon - that refers to "the icon". When the device is running on a high-density screen, R.drawable.icon "points" to the hi-density version of the icon; and on a low-density device, R.drawable.icon "points" to the low-density version.

现在,用于实际解决资源名称的机制超出了本文的范围。但这里是结果是:每个版本都有的不同的路径的,但是的资源ID 的是的所有变种!因此,我们必须通过唯一的ID参考资源。我们不能路径是指资源,我们甚至无法列出的路径 - 那会破坏资源的目的开始。我们不应该需要。我们要始终知道我们需要,我们只知道它的变体,我们会得到,的的资源,我们不在乎的(至少,我们不应该)。

Now, the mechanisms used to actually resolve the Resource name is beyond the scope of this post. But here is the upshot: each variant has a different path, but the Resource ID is the same for all variants! Consequently, we must refer to resources via ID only. We can’t refer to resources by path, and we can’t even list the paths out – that would defeat the purpose of Resources to begin with. And we shouldn’t need to. We should always know which resources we need, we just know which variant we'll get, and we don't care (at least, we shouldn’t).

这就是为什么你这样的巡航能力麻烦查询资源/ RES /原料,甚至让目录本身。这些功能都违背了资源的概念。根据定义,你总是知道资源你要提早什么 - 连同它的资源ID

This is why you are haveing such trouble querying the resources in /res/raw, or even getting to the directory itself. Those functions are contrary to the concept of Resources. By definition, you always know what Resource you want ahead of time - and the Resource ID that goes with it.

所以,如果你不知道你想要什么,所以想弄清楚什么是可用的?好了,这就是的 /资产的位置的目的。注意
/资产是资源层次的的一部分。就在这里之间的区别/ RES /原料 /资产的东西 /资产
没有资源
的。

So, what if you don’t know what you want, and therefore want figure out what’s available? Well, that’s the purpose of the /assets location. Note that /assets is not part of the resource hierarchy. And therein lies the distinction between the /res/raw and /assets: things in /assets are not resources.

由于东西放在 /资产都没有资源,他们没有资源ID。所以,你的必须的其路径是指他们。事实上,既然你
不知道在哪里的 /资产文件夹本身所在,必须通过他们的相对的路径是指他们。进入 AssetManager AssetManager (一)知道如何找到你的应用程序的 /资产文件夹,(b)如何列出 /资产文件夹,及(c)下的文件和文件夹如何打开在 /资产文件夹下的文件和文件夹。要看到这是如何实现的,请参阅该 AssetManager 类,及后如果再需要进一步的指导。

Since things placed in /assets are not resources, they do not have Resource IDs. So you must refer to them by their path. In fact, since you don’t know where the /assets folder itself is located, you must refer to them by their relative path. Enter AssetManager. AssetManager (a) knows how to find the your application’s /assets folder, (b) how to list files and folders under the /assets folder, and (c) how to open files and folders under the /assets folder. To see how this is done, please review the docs for the AssetManager class, and post again if you need further guidance.

因此​​,在总结:如果你有一个文件,这是一个资源,然后将其放在 / RES /原料,并获得通过它它的资源ID。如果该文件是不是资源,然后把它放在了 /资产文件夹,并通过其路径访问它。

So, in summary: if you have a file that’s a Resource, then put it in /res/raw, and access it via it’s resource ID. If the file is not a resource, then put it somewhere in the /assets folder, and access it via its path.

有关资源的更多信息,请参见应用程序资源页面中 Android开发者指南

For more information on resources, see the Application Resources page in the Android Developers Guide

这是否让事情更清晰?

这篇关于安卓资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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