Android Raw Folders - Creation and Reference

Android Raw folders - creation and reference

The raw folder must be inside the res folder, otherwise it won't work.

Where is the file directory res/raw?

You need to create new directory named raw inside res folder of your project. Then put your .mp3 file inside res>raw folder of your project

I am using MediaPlayer for custom sound. And This is Working fine for me. This is working for all devices.

private MediaPlayer player;

For play custom sound:

 try {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);

player = MediaPlayer.create(this, uri);
player.setLooping(true); // This will play sound in repeatable mode.
player.start();

} catch (Exception e) {
e.printStackTrace();
}

For stop sound:

  if (player != null)
player.stop();

This is working for me. Hope this will also helps you.

How to reference a File in raw folder in Android

here are 2 functions. one to read from RAW and one from the Assets

/**
* Method to read in a text file placed in the res/raw directory of the
* application. The method reads in all lines of the file sequentially.
*/

public static void readRaw(Context ctx,int res_id) {

InputStream is = ctx.getResources().openRawResource(res_id);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
// size

// More efficient (less readable) implementation of above is the
// composite expression
/*
* BufferedReader br = new BufferedReader(new InputStreamReader(
* this.getResources().openRawResource(R.raw.textfile)), 8192);
*/

try {
String test;
while (true) {
test = br.readLine();
// readLine() returns null if no more lines in the file
if (test == null)
break;
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}

}

and from Assets folder

/**
* Read a file from assets
*
* @return the string from assets
*/

public static String getQuestions(Context ctx,String file_name) {

AssetManager assetManager = ctx.getAssets();
ByteArrayOutputStream outputStream = null;
InputStream inputStream = null;
try {
inputStream = assetManager.open(file_name);
outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
} catch (IOException e) {
}
return outputStream.toString();

}

Android Studio create A folder in R.raw

You can not add folders to the raw folder or any of the folders inside the res folder.

Android supports only a linear list of files within the predefined folders under res.
The asset folder though, can have an arbitrary hyarchie of folders because the asset folder is not considered as resources.

Android: How to add R.raw to project?

Adding a raw folder to your resource folder (/res/) should do the trick.

Read more here:
https://developer.android.com/guide/topics/resources/providing-resources.html

Access resources folder and list of files in raw folder in package in Android

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

How to acess new created folder in android studio?

I created a folder in res folder and it named draw2 which i will use for specisific imageviews.

Custom folders in res folder are not supported. Better To create raw folder or asset folder
for this purpose

raw/

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.

How to create assets in android studio

How to create raw in android studio

for more information The Android Project View

module-name/

build/

libs/

src/
androidTest/
main/
AndroidManifest.xml
java/
jni/
gen/
res/
assets/
test/
build.gradle (module)
build.gradle (project)


Related Topics



Leave a reply



Submit