Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Path to Subdirectory in Resources Folder

i'm trying to get an array of all png files in a subdirectory of the Resources Folder. In my apps Resources Folder I have created a Folder named "images". This folder holds all the png-files I need to display.

This is the way I tried to get the Path:

    NSString *imagepath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],@"images/"];

NSLog(@"Path to Images: %@", imagepath);

NSArray * paths = [NSBundle pathsForResourcesOfType: @"png" inDirectory:imagepath];
NSMutableArray * allImageNames = [[NSMutableArray alloc] init];

for ( NSString * path in paths )
{
    if ( [[path lastPathComponent] hasPrefix: @"AQ"] )
        continue;

    [allImageNames addObject: [path lastPathComponent]];
}

This way I get a Path like …/appname.app/images

But if i try to do it that way, the array is always empty. What am I doing wrong?

greetz, Zarak

like image 608
DevZarak Avatar asked Sep 07 '11 13:09

DevZarak


People also ask

How to get path to resources in java?

The simplest approach uses an instance of the java. io. File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file.

How to read files from resource folder?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().


1 Answers

Just solved it.

NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png"
 inDirectory:@"random pictures"];

 NSLog(@"pngs in my dir:%@", pngs);

dir structure: "Resources/random pictures".

This works, BUT, when you add the files to 'Resources', you need to check "Create folder references for any aded folders", and NOT "Create groups for any added folders".

important step

Cheers!

like image 125
codrut Avatar answered Oct 22 '22 12:10

codrut