- Resource Raw Folder in Android Studio
- How the Resource raw folder is different from the Assets folder?
- But when to use which folder?
- How to Create Resource Raw Folder in Android Studio?
- Get path of Android resource
- 3 Answers 3
- Multiple Resource Folders In Android
- Step to Create Multiple Resource Folders in Android
- Step 1 — Create a parent folder that contains all layouts of our project
- Step 2 — Create The Features Folder
- Step 3 — Adding Resource Folder For The Features
- Step 4 — Android Resource Folders Into Android Resources
- How to create new res folder in Android Studio
- 8 Answers 8
- Assets Folder in Android Studio
- How the asset folder is different from the Resource Raw folder?
- But when to use which folder?
- How to Create Assets Folder in Android Studio?
Resource Raw Folder in Android Studio
The raw (res/raw) folder is one of the most important folders and it plays a very important role during the development of android projects in android studio. The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw. So we will simply create it inside the res folder. But before creating a raw folder let’s have a look at the asset folder in android which acts the same role as the raw folder in android studio. So let discuss how the resource raw folder is different from the assets folder?
How the Resource raw folder is different from the Assets folder?
In Android one can store the raw asset file like JSON, Text, mp3, HTML, pdf, etc in two possible locations:
- assets
- res/raw folder
Both of them appear to be the same, as they can read the file and generate InputStream as below.
But when to use which folder?
Below is some guidance that might be helpful to choose
1. Flexible File Name: (assets is better)
- assets: The developer can name the file name in any way, like having capital letters (fileName) or having space (filename).
- res/raw: In this case, the name of the file is restricted. File-based resource names must contain only lowercase a-z, 0-9, or underscore.
2. Store in subdirectory: (possible in assets)
- assets: If the developer wants to categories the files into subfolders, then he/she can do it in assets like below.
3. Compile-time checking: (possible in res/raw)
- assets: Here, the way to read it into InputStream is given below. If the filename doesn’t exist, then we need to catch it.
- res/raw folder: Here, the way to read it into InputStream is:
So putting a file in the res/raw folder will provide ensure the correct file-name during compile time check.
4. List filenames at runtime: (possible in assets)
- assets: If the developer wants to list all the files in the assets folder, he/she has used the list() function and provide the folder name or ” “ on the root folder as given below.
- res/raw: This is not possible in this folder. The developer has to know the filename during development, and not runtime.
So, in assets, one can read the filename during runtime, list them, and use them dynamically. In res/raw, one needs to code them ready, perhaps in the string resources file.
5. Filename accessible from XML: (possible in res/raw)
So if you need to access your file in any XML, put it in the res/raw folder. Let’s make a table to remember the whole scenario easily.
Res/Raw Folder | ||||||
---|---|---|---|---|---|---|
Flexible File Name NO | ||||||
Store in subdirectory NO | ||||||
Compile-time checking YES | ||||||
List filenames at runtime NO | ||||||
Filename accessible from XML How to Create Resource Raw Folder in Android Studio?Now let’s discuss how to create the Resource Raw folder in the android studio. Below is the step-by-step process to create the Resource Raw folder in Android studio. Step 1: To create the Resource Raw folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Open your android studio go to the app > res > right-click > New > Android Resource Directory as shown in the below image. Step 3: Then a pop-up screen will arise like below. Here in Resource type choose raw. Step 4: After choosing the raw from the dropdown menu click on the OK button and keep all the things as it is. Step 5: Now you can see the raw folder has been created and you can find the folded in the app > res > raw as shown in the below image. Источник Get path of Android resourceMy question is: is it possible to get the absolute path of a resource (in a subdirectory of the res/ folder) in Android? Most of the answers I see to this question on google suggest getting a file descriptor, implying that this is not possible. EDIT: The reason I want to do this is because I’m writing classes that accept a path to media and play it. Testing would be much easier if I could pass the absolute path of a resource. 3 Answers 3Use URI Paths instead of «absolute» path, see this post Or use openRawResource(R.id) to open an inputStream, and use it the same way you would use a FileInputStream (readonly) Not possible. The resource folder you see is compiled into the apk, and it may not even store as it is. (e.g. those layout xml files are no longer xml files once it is compiled) Two possible solution/work-around for your situation:
In general, NO. As it was repeated several times here, you need to access them through the resources. That said, there are ways you can work around that: Источник Multiple Resource Folders In AndroidYou can manage your resources XML files (layout, drawable. ) better, by grouping them into separate subfolders corresponding to the app’s features. Table of Contents Step to Create Multiple Resource Folders in Android
Step 1 — Create a parent folder that contains all layouts of our projectSwitch project tree explorer to Project view. Right click in res,select New → Directory, name it as layouts. Step 2 — Create The Features FolderSelect folder you created, Then right click and select New → Folder → Res Folder. Name the folder based on your feature. In my case, I have named as user, details. Step 3 — Adding Resource Folder For The FeaturesThen, Create resource folders like layout, drawable, menu,anim,etc for the created feature folders. In my example, I have created layout and drawable resource folders. Step 4 — Android Resource Folders Into Android ResourcesDmytro Danylyk (A Google Developers Experts) says, That we can have multiple res folders within by add resource sets. Источник How to create new res folder in Android StudioI want to add localized strings for my android app. Therefore I need a values-xx folder in my Res folder. The original values folder has a blue dot, so I tried creating a new Package, but a package can’t contain a hyphen so this must be wrong. Instead I tried right-clicking res and choosing New -> Android resource directory, but this time nothing happens. No dialog or reaction of any sort as I can see. How do I create a values-xx folder? Edit: I can create the folder from file explorer and it all works good. It is just irritating to not be able to do it from inside Android Studio. Edit2: This bug is fixed in newer versions of Android Studio. 8 Answers 8When you are in the Android view (rather than Project or Packages) in Android studio, you just need to right click the «values» directory and choose New > «Values resource file». That gives you a chose of different resources you can add. For example, if you want to add a different language to your app you can choose the Language option and press the «>>» button. If you want Swahili then select that from the list, type «strings» for the file name, and press OK. Android Studio will automatically create a values-sw directory with the new strings.xml file in it. And in your Android file view you conveniently see both strings files together. And it is a similar process for adding other types of resources (see my other example). You no longer have to manually add the directories (but you can do that too by right clicking the res directory and choosing New > Android resource directory). Источник Assets Folder in Android StudioIt can be noticed that unlike Eclipse ADT (App Development Tools), Android Studio doesn’t contain an Assets folder in which we usually use to keep the web files like HTML. Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as “resources“, Android will treat them into its resource system and you will be unable to get the raw data. If one wants to access data untouched, Assets are one way to do it. But the question arises is why in the asset folder? We can do the same things by creating a Resource Raw Folder. So let discuss how the assets folder is different from the Resource Raw folder? How the asset folder is different from the Resource Raw folder?In Android one can store the raw asset file like JSON, Text, mp3, HTML, pdf, etc in two possible locations:
Both of them appears to be the same, as they can read the file and generate InputStream as below But when to use which folder?Below is some guidance that might be helpful to choose 1. Flexible File Name: (assets is better)
2. Store in subdirectory: (possible in assets)
3. Compile-time checking: (possible in res/raw)
So putting a file in the res/raw folder will provide ensure the correct file-name during compile time check. 4. List filenames at runtime: (possible in assets)
So, in assets, one can read the filename during runtime, list them, and use them dynamically. In res/raw, one needs to code them ready, perhaps in the string resources file. 5. Filename accessible from XML: (possible in res/raw) So if you need to access your file in any XML, put it in the res/raw folder. Let’s make a table to remember the whole scenario easily.
|