简体   繁体   English

Android 项目中 getResourceAsStream 的路径

[英]Path for getResourceAsStream in Android Project

I am trying to programmatically load a .png file but I am not sure what is the exact path I should be giving.我正在尝试以编程方式加载 .png 文件,但我不确定我应该提供的确切路径是什么。 I tried different paths that I think would get me to the resource but all return null:我尝试了不同的路径,我认为这些路径可以让我找到资源,但都返回 null:

InputStream is = getClass().getClassLoader().getResourceAsStream("playstore.png");
Bitmap bmp = BitmapFactory.decodeStream(is);
mStoreImage.setImageBitmap(bmp);

If I understand correctly I cannot use resources as I am building a .jar.如果我理解正确,我将无法使用资源,因为我正在构建一个 .jar。 This is my project package structure displayed with Android Studio:这是我用 Android Studio 显示的项目包结构: 在此处输入图片说明

You should use Resources.openRawResource()你应该使用Resources.openRawResource()

eg if your Activity例如,如果您的活动

getResources().openRawResource(R.raw.playstore.png);

remember to put the file under res/raw记得把文件放在res/raw

Edit:编辑:

In addition, android has it own method to get drawable (eg a png file) into Bitmap此外,android 有自己的方法可以将 drawable(例如 png 文件)转换为 Bitmap

Android Studio Gradle projects read files from a separate resources directory in the same way as Maven does it. Android Studio Gradle 项目以与 Maven 相同的方式从单独的资源目录中读取文件。

Create a "resources" folder in Windows Explorer (Android Studio don't allow you to do that) and then recreate the package path and place files there, eg:在 Windows 资源管理器中创建一个“资源”文件夹(Android Studio 不允许你这样做),然后重新创建包路径并将文件放在那里,例如:

A class located at this path:位于此路径的类:

C:\...\app\src\main\java\com\adscendmedia\videosdk\AdscnedAPI.java

Read resource files from this path:从此路径读取资源文件:

C:\...\app\src\main\resources\com\adscendmedia\videosdk\playstore.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM