你可以在Android Studio中引用资源路径如何?

huangapple go评论65阅读模式
英文:

How can you reference path to resources in Android Studio?

问题

你好,以下是代码部分的翻译:

// 尝试加载图片并创建动画的函数
fun loadPics(context: Context, path: String, iconList: MutableList<Bitmap>): MutableList<Bitmap> {
    val assetManager = context.assets
    try {
        var `is`: InputStream
        var files = assetManager.list(path)
        for (i in files!!.indices) {
            `is` = assetManager.open(path + files[i])
            val bitmap = BitmapFactory.decodeStream(`is`)
            iconList.add(i, bitmap)
        }
    } catch (e: IOException) {
        e.printStackTrace()
    } finally {
        return iconList
    }
}

请注意,代码中的 <Bitmap> 不需要翻译。

至于你的问题,你想要引用路径的方式是正确的,但在 Android Studio 中,资源路径应该以 "res/" 开头,而不是绝对路径。例如,你可以引用 drawable 资源的路径如下:

res/drawable/

这应该是正确的资源路径格式。如果你在使用 assetManager 时仍然遇到问题,可能需要检查路径是否正确,并确保资源位于指定的位置。

英文:

Hi I am trying to load series of pictures to create animation in Android Studio. I managed to spot similar thread and i got to this stage:

fun loadPics(context: Context, path: String, iconList: MutableList&lt;Bitmap&gt;): MutableList&lt;Bitmap&gt;{
    val assetManager = context.assets
    try
    {
        var `is`: InputStream
        var files = assetManager.list(path)
        for(i in files!!.indices) {
            `is` = assetManager.open(path + files[i])
            val bitmap = BitmapFactory.decodeStream(`is`)
            iconList.add(i, bitmap)
        }
    }
    catch (e: IOException)
    {
        e.printStackTrace()
    }
    finally {
        return iconList
    }
}

My really basic question is how do I reference path? i tried right - clicking on resource and copying path / reference but assetManager always returns nothing..

Tried copying paths by righ clicking resource files
res/drawable/
res/
absolute path C:\Users..\AndroidStudioProjects\ImageViewer\app\src\main\res\drawable\

答案1

得分: 0

以下是您要翻译的内容:

唯一可以作为InputStream打开的资源是原始资源和资产。不会对其他目录中的资源(如res/drawable)起作用。

您可以将文件放在res/raw目录下,然后通过context.resources.openRawResource()使用它们的R.raw ID作为InputStream来访问它们。原始资源的优点是它们可以根据设备和语言环境特性进行组织,就像布局和字符串资源一样,因此系统会自动选择文件的适当版本。例如,您可以有一个res/raw/myFile.txtres/raw-large/myFile.txt,它将为大屏设备提供专用的文件。此外,如果将应用程序捆绑到Google Play,商店可以根据设备自定义下载,因此不会包含永远不会在特定设备上使用的原始文件。

您可以将文件放在src/main/assets(您可能需要创建此目录),然后通过context.resources.assets.open()作为InputStream来访问它们。资产的优点是您可以在src/main/assets内部使用子目录进行组织。您可以使用context.resources.assets.list()列出资产文件。


资产示例:

您在这里有一张图片:src/main/assets/picture.png

@Throws(IOException::class)
fun Context.loadBitmapAsset(fileNameInAssets: String): Bitmap {
    val stream = assets.open(fileNameInAssets)
    return BitmapFactory.decodeStream(stream)
}

如果您调用loadBitmapAsset("picture.png"),它将加载您的picture.png

或者,如果您想要将res/main/assets中的所有文件作为图像打开:

@Throws(IOException::class)
fun Context.loadAllAssetsAsBitmaps(): List<Bitmap> {
    return assets.list("") // 我们在资产的根目录中使用空字符串
        .map { BitmapFactory.decodeStream(assets.open(it)) }
}

原始资源示例:

您在这里有一张图片:src/main/res/raw/picture.png

@Throws(IOException::class)
fun Context.loadRawBitmap(@RawRes resId: Int): Bitmap {
    val stream = resources.openRawResource(resId)
    return BitmapFactory.decodeStream(stream)
}

如果您调用loadBitmapAsset(R.raw.picture),它将加载您的picture.png

或者,如果您想要将res/main/res/raw中的所有文件作为图像打开:

@Throws(IOException::class)
fun Context.loadAllRawResourcesAsBitmaps(): List<Bitmap> {
    val rawIds = R.raw::class.java.fields.map { it.int }
    return rawIds.map { BitmapFactory.decodeStream(resources.openRawResource(it)) }
}
英文:

The only resources you can open as an InputStream are raw resources and assets. It won't work for resources in other directories like res/drawable.

You can put files in res/raw and then access them as an InputStream via context.resources.openRawResource() using their R.raw ID. The advantage with raw resources is that they can be organized by device and locale characteristics just like layout and string resources, so the system takes care of selecting the appropariate flavor of the file. e.g. you could have a res/raw/myFile.txt and res/raw-large/myFile.txt and it will give you the specialized file on large screen devices. Also, if you bundle your app for Google Play, the store can tailor the download for the device so raw files that will never be used on a specific device won't be included.

You can put files in src/main/assets (you might have to create this directory) and access them as an InputStream via context.resources.assets.open(). The advantage with assets is that you can organize them with subdirectories inside src/main/assets. You can list asset files using context.resources.assets.list().


Assets Example:

You have an image here: src/main/assets/picture.png.

@Throws(IOException::class)
fun Context.loadBitmapAsset(fileNameInAssets: String): Bitmap {
    val stream = assets.open(fileNameInAssets)
    return BitmapFactory.decodeStream(stream)
}

If you call loadBitmapAsset(&quot;picture.png&quot;) it will load your picture.png.

Or if you want to open all files in res/main/assets as images:

@Throws(IOException::class)
fun Context.loadAllAssetsAsBitmaps(): List&lt;Bitmap&gt; {
    return assets.list(&quot;&quot;) // we use empty string for the root directory in assets
        .map { BitmapFactory.decodeStream(assets.open(it)) }
}

Raw resource example:

You have an image here: src/main/res/raw/picture.png

@Throws(IOException::class)
fun Context.loadRawBitmap(@RawRes resId: Int): Bitmap {
    val stream = resources.openRawResource(resId)
    return BitmapFactory.decodeStream(stream)
}

If you call loadBitmapAsset(R.raw.picture) it will load your picture.png.

Or if you want to open all files in res/main/res/raw as images:

@Throws(IOException::class)
fun Context.loadAllRawResourcesAsBitmaps(): List&lt;Bitmap&gt; {
    val rawIds = R.raw::class.java.fields.map { it.int }
    return rawIds.map { BitmapFactory.decodeStream(resources.openRawResource(it)) }
}

huangapple
  • 本文由 发表于 2023年5月17日 20:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定