Loading files from raw folder in Android - android

Suppose I store various images in raw folder. However, my own code first learns of the file names at runtime. (It reads a text configuration file at startup.) Is it possible to then search "raw" folder in my android app and if found, load image from raw into imageview?

Okay, thanks to Mario I got on track to doing some research. These 5 SO's explain everything:
Android images in /assets or res/raw
Reading assets or raw or resource files as a File object in Android
How to reference a File in raw folder in Android
Check for file existence in androids assets folder?
Difference between /res and /assets directories

Related

How to provide a text file within an Android app?

Where do I put a text file that I want deployed with my app such that I can provide within my app a path to that text file. I have an included JNI library which will take the text file at that path and perform actions on it. So in other words, I don't think I can just put it in my assets folder without reading the file and resaving it to SD card or something (as I don't think you can reference an assets file directly by path right?). Is there a way around this?
You can place files in res/raw/ and read them as raw resource files/streams.
See https://stackoverflow.com/a/15934525/5734895 for reading raw resources.

Android traverse the raw folder

I am a novice programmer,and encounter a problem in the development of an Android application.I have a subfolder raw under the folder res, now ,i want to traverse the folder raw and get files' name and path in the folder. who can help me?
There are some MP3 files in the raw folder ,and i want to list of their name and get their path for use VideoView to play it. poor English, forgive me,thanks!
If you want to be able to get a list of files shipped with the app, move the files in the assets folder.
For example : if you create a folder in the assets folder called 'video' then you can retrieve a string array with the filenames like this :
String[] filesInAssets = getResources().getAssets().list("video");
I don't see a solution for reading the files under the res folder.

getting all names of files in internal storage of android phone

I read a tutorial on how to get names of files in an absolute path, but now I want to get all files - for example in my internal storage-
I mean by all the files, the files which are in the folders not only in an absolute path.
for example if my internal have folders and files like that
/music
/videos
/data
mine.png
mohamed.txt
and my music folder have files like
him.png
sir.doc
I want a list like that
mine.png
mohamed.txt
him.png
sir.docx
I hope some one got me.
Thanks

Can I create folder for storing files besides RAW folder?

can i create folders, besides raw folder, for storing different kind of files?
For examples, my application need to count all the images put in a folder.
So, I need a specific folder for images only, and other folders for sounds, for .txt etc!
But when i tried to create a new folder, I couldn't reach it :(
It is just a hunch, but you might have more luck using the assets folder instead. You can certainly use it to store embedded web sites which have subfolders.
In general you cant create custom resource folders. But you can use assets dir and place your resources there. You can make as much subfolders in assets dir as you need. But android won't help you to manage your files in assets dir. Read this.

When should I use the assets as opposed to raw resources in Android?

I'm in the mid of my Android studies, and I just covered the Assets and Raw resources. I'm trying to understand the reason for using Raw resources vs. Assets.
They both provide with an uncompiled resource input stream.
It seems that Assets provide much more flexibility and functionality than Raw resources.
a. You can create folder structures under assets but not under raw
b. You can list all resources dynamically in the assets folder but not in the raw folder.
So, why would I use Raw resources in Android?
The main differences between the raw folder and the assets folder.
Since raw is a subfolder of Resources (res), Android will
automatically generate an ID for any file located inside it. This
ID is then stored in the R class that will act as a reference to
a file, meaning it can be easily accessed from other Android classes
and methods and even in Android XML files. Using the automatically
generated ID is the fastest way to have access to a file in Android.
The assets folder is an “appendix” directory. The R class does
not generate IDs for the files placed there, which is less compatible
with some Android classes and methods. File access in the assets folder is slower since you will need to get a handle to it
based on a String. However some operations are more easily done by
placing files in this folder, like copying a database file to the
system’s memory. There’s no (easy) way to create an Android XML
reference to files inside the Assets folder.
From the Android documentation, the raw/ directory is used for:
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.
In one line, the files in the raw/ directory are not compiled by the platform, are assigned a resource ID and cannot be grouped into sub-folders whereas if you want the otherwise use the assets/ directory.
Adding to the answers given above...
/res/strings,/res/layout,/res/xml files etc all gets compiled into binary format. But if you place files, including XML files, in the /res/raw/ directory instead, they don’t get compiled into binary format.
One big advantage of using assets over raw resources is the
file:///android_asset/ Uri prefix.This is useful for loading an
asset into a WebView. For example, for accessing an asset located in
assets/foo/index.html within your project, you can call
loadUrl("file:///android_asset/foo/index.html") loading that HTML
into the WebView.

Categories

Resources