I have a huge directory of infrared remote control codes, and I'd like to parse through each of them when needed. However, I am having issues trying to access the folders I have in my application's res/raw/ folder. I'd like to be able to get the folder names, because that would essentially sort for me and list out all the brands that I'd like to list in a Spinner, and then pull files from inside that folder later.
What's wrong with me doing something like this?
String path = "android.resource://" + getPackageName() + "/res/raw/";
File f = new File(path);
File[] files = f.listFiles();
for(File inFile : files){
if(inFile.isDirectory()){
Log.d(TAG, inFile.getName());
}
}
Just found out that I can't create subdirectories in my res/raw folder, and I'd have to go about this through the assets folder. Would this be as simple as this structure?
assets
- remotes
- Samsung
- remote1.txt
- remote2.txt
- Sony
- remote1.txt
- etc
However, I am having issues trying to access the folders I have in my application's res/raw/ folder.
You cannot have folders inside of res/raw/. A resource directory can contain files, not subdirectories.
If you want to package a directory tree with your app, use assets/ and AssetManager, not raw resources.
Related
In a testcase I need to load a picture which is available in my testproject in the folder
/myproject/res/drawable-hdpi/attachment.png
I tried it already by using the InstrumentationTestCase and storing the file in the /res/raw folder and loading it with this, but the R file does not contain my attachment.png
Context testContext = getInstrumentation().getContext();
InputStream input = testContext.getResources().openRawResource(R.raw.?????????)
any idea?
thanks
You should not use raw to save images, used drawable o asset ;)
Difference between /res and /assets directories
Android images in /assets or res/raw
I want to be able to access all of the files in the raw folder of the resources folder in my Android application package. I have created a function that gets an array of files from a folder in the external storage directory, but I want to do the same thing for the files in the raw folder of the resources folder. I have looked at different suggestions, but most people seem to want to access a specific file. I only want to access the folder and then create an array of files in that folder. Please could anyone help me with this to find a solution. Thanks in advance.
Here is the code for my other function that I have created:
public static List<String> getListOfSavedFileNames()
{
File folder = new File(Environment.getExternalStorageDirectory() + "/XML/");
List<String> listOfFileNames = new ArrayList<String>();
if(folder.exists())
{
File[] listOfFiles = folder.listFiles();
for(File f : listOfFiles)
{
String fileName = f.getName();
String finalFileName = FileExtentionFunctions.removeExtention(fileName);
listOfFileNames.add(finalFileName);
}
}
else
{
}
return listOfFileNames;
}
There are no files. Raw resources are resources, not files.
You are welcome to:
Have a "table of contents" resource that lists the other ones, as a <string-array> or an XML resource or something
Use reflection to iterate over the R.raw values
My application is mostly c++ (using NDK) so I use fopen, fwrite, etc. standard functions to create and game save files and write into them.
When I use fopen("game.sav", "wb"), it appears that it's being created at path
/data/user/10/com.my.game/files/game.sav.
My app is multi-user. So I want to have a separated folders where users store their save-files. And instead of the path above I'd like to have paths like
/data/user/10/com.my.game/files/user0/game.sav,
/data/user/10/com.my.game/files/user1/game.sav, etc
My app's frontend is in Java, and when new user is being registered, I want to create a folder /data/user/10/com.my.game/files/user0/. But I don't know how to do it, because
final File newDir = context.getDir("user0", Context.MODE_PRIVATE);
results in path being created at /data/user/10/com.my.game/app_user0 that's a different path.
It is possible to create folders at /data/user/10/com.my.game/files/ and how ?
Simple way to do it, this code you can change it suit many conditions. If you know that your path is different from what getFilesDir() gets you then you can create a File first of all by using a path that you know and the last 2 lines of code will still be same.
File file = this.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + "/foo"); // foo is the directory 2 create
newfile.mkdir();
And if you know the path to "files" directory:
File newfile2 = new File("/data/data/com.example.stackoverflow/files" + "/foo2");
newfile2.mkdir();
Both code works.
Proof of Working:
How do i make directories in internal storage?
I tried this:
File file = getFilesDir();
this makes me goes to folder "/data/data/com.mypackages/files/"
Then i want to make a folder again in that directories, let's say i want to make "myfiles" folder in there so it becomes, "/data/data/com.mypackages/files/myfiles/".
Can anyone tell me how?
I also tried this:
File file = getDir("myfiles", MODE_PRIVATE);
It makes the folder, but it was created with "app_", so the directories becomes "/data/data/com.mypackages/app_myfiles". I don't want that because i can't read the folder if it has "app_" in there.
The solution is under your eyes :D
m_applicationDir = new File(this.getFilesDir() + "");
m_picturesDir = new File(m_applicationDir + "/pictures");
With this code, i save in m_applicationDir the dir of the package (in your case the dir saved in file).
Then simply create a sub-directory named pictures.
So m_picturesDir points to:
/data/data/com.mypackages/files/pictures
In my raw folder I have a number of folders, how can I target these, Ive tried a path var as below but no luck:
String path = "R.raw.myFolder";
mSoundManager.addSound(1, path.myFile);
I do not understand why you have folders inside your raw folder. That folder is there to store the media you need. Do not over complicate the project by having to reference a folder name on top of the file name. the code below is for accessing files straight out of the raw folder.
Try giving this a go:
mSoundManager.addSound("android.resource://"+getPackageName()+ "/" + R.raw.filename);
I am not sure if this is what you are looking for but this is what works for me for all my raw files.
I left out a piece. Code is now correct.