I´m creating a game for my college project and I was wondering if I can get the names(just the names) of the images I put inside the /drawable folder in android. It can also be the files I put inside the /raw folder, the option that works better.
Use the following snippet
Field[] ID_Fields = R.drawable.class.getFields();
for (Field f : ID_Fields) {
try {
System.out.println(f.getName() + "sample");
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
Related
In android, I need to load a set of images from res/drawable directory
I use the following code:
Field[] fields = R.drawable.class.getFields();
List<Picture> pictures = new ArrayList<>();
for (Field field : fields) {
String name = field.getName();
if (name.startsWith("img")) {
Picture picture = new Picture(name);
pictures.add(picture);
}
}
Among all files in 'drawable', it finds all files starting as a string 'img'.
For this code to work correctly, I have to manually change the names of image files by myself.
If I can get the extension of each resource in drawable (such as jpg, png, etc.), I don't need to change the file name like this.
Is there any way to achieve this?
I appreciate your help, thanks :D
I answer my question.
I moved all the image files into "asset/imgs"
and use the following code to load them
List<Picture> pictures = new ArrayList<>();
try {
String[] images = assetManager.list("imgs");
for (String name : images) {
Picture picture = new Picture(name);
pictures.add(picture);
}
} catch (IOException e) {
// you can print error or log.
throw new RuntimeException(e);
}
I want to read all the Images in drawable folder as File object and store them in an array of File.
Is there a way to get list of all images in drawable and read them as File?
Edit: I need to know the following..
How to collect all the images from drawable folder. My images have different meaningful name based on the content. There is no common pattern in the name.
How to read these images as File.
No, because they are not files on the device. They are only files on your development machine. Resources are entries in the APK itself at runtime.
First you need to convert all drawable to bitmap and save to Local Storage then read it as File
You can get all Drawables from this method
public Drawable[] getAllDrawables()
{
Field[] ID_Fields = R.drawable.class.getFields();
Drawable[] drawables = new Drawable[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
try {
drawables[i] = ContextCompat.getDrawable(this,ID_Fields[i].getInt(null));
} catch (IllegalArgumentException | IllegalAccessException ignored) {
return null;
}
}
return drawables;
}
Possible Duplicate
Here
I've created a folder 'test' inside res and I want to display them in an image view. How exactly can I fetch the image with a given name in the designated folder?
ImageView test = (ImageView) testing.findViewById(R.id.test);
flag.setImageDrawable(getResources().); <==== This?
EDIT
InputStream is = null;
try {
is = this.getResources().getAssets().open("country_flags/sample.png");
} catch (IOException e) {
;
}
image = (ImageDrawable) BitmapFactory.decodeStream(is);
try {
flag.setImageDrawable(getResources().getAssets().open("country_flags/"+nationality+".png"));
} catch (IOException e) {
e.printStackTrace();
}
How exactly can I fetch the image with a given name in the designated folder?
You don't. You cannot invent new resource types, and so your test directory will, at best, be forever ignored.
I need some help. I need to iterate through assets folder and find all files and subfolders and do something with them.
For example . I have created folder "myicons" in assets folder. But this folder(myicons) include other folders which include icons. It looks like
/myicons/buttons/playbt.ico ,/myicons/buttons/stopbt.ico ,
/myicons/shapes/square.ico
and so on.
So I need to iterate through every subfolder but it doesn't work. AssetsManager.list(path) returns only files .
Please suggest how to solve the problem. For example if getFromAssets(getAsset(),"myicons");
void getFromAssets (AssetManager mgr, String path) {
try {
String list[] = mgr.list(path);
if (list != null)
for (int i=0; i<list.length; ++i)
{
File file = new File(list[i]);
if (file.isDirectory()) {
Log.d("Assets:",list[i]+ "is Directory");
}
else {
Log.d("Assets:",list[i]+ "is File");
}
}
} catch (IOException e) {
Log.v("List error:", "can't list" + path);
}
}
The problem was that if I have my_icons folder and in this folder I have buttons folder it didn't recognize buttons folder.
list method doesn't recognize EMPTY FOLDERS. Everything works if folder is not empty .
Thx everyone for help.
I will post code a bit later .
Team,
I would like to know, whether we can add custom configurations in Manifest or deploy properties? If yes, please provide examples.
thanks,
Ramesh
Put your .properties file into the /res/raw directory
try {
Resources resources = this.getResources();
// Check generated R file for name oy your resource
InputStream rawResource = resources.openRawResource(R.raw.resourceFileName);
Properties properties = new Properties();
properties.load(rawResource);
System.out.println("The properties are now loaded");
System.out.println("properties: " + properties);
} catch (NotFoundException e) {
System.err.println("Did not find raw resource: "+e);
} catch (IOException e) {
System.err.println("Failed to open property file");
}
Source: http://myossdevblog.blogspot.com/2010/02/reading-properties-files-on-android.html