panthro
panthro

Reputation: 24061

Raw folder url path?

How can I get the URL of the raw folder? I do not want to access my asset via id but by path instead.

Upvotes: 56

Views: 101692

Answers (5)

Abhishek Dharmik
Abhishek Dharmik

Reputation: 101

A Generic 100% Working Solution

Step 1. Create an Extensions.kt file

Step 2. Add the below function in Extensions.kt

fun Activity.getRawFolderPath(video: Int) =
Uri.parse("android.resource://" + this.getPackageName().toString() + "/" + video)

and call it from Activity like: getRawFolderPath(R.raw.video)

or from Fragment Like: requireActivity().getRawFolderPath(R.raw.video)

Upvotes: 1

Karthi
Karthi

Reputation: 13752

Uri video = Uri.parse("android.resource://com.cpt.sample/raw/filename");

Using this you can access the file in raw folder, if you want to access the file in asset folder use this URL...

file:///android_asset/filename

Make sure you don't add the extension to the filename. E.g. "/movie" not "/movie.mp4".

Upvotes: 88

M. Usman Khan
M. Usman Khan

Reputation: 4408

Now u can do this:

getResources().openRawResource(R.raw.filename)

Upvotes: 10

Mario Velasco
Mario Velasco

Reputation: 3456

This is to find a file named video.mp4 inside raw folder:

    String path = "android.resource://" + getPackageName() + "/" + R.raw.video;

Upvotes: 14

Lukap
Lukap

Reputation: 31963

The point with using raw is to access with the id, for example R.raw.your_object_id. If you want to access to your resources with file path then you need to use assets.

Then you can use assets like this:

InputStream myInput = myContext.getAssets().open("MyFolder/" + "MyFile.db3");
InputStream myInpu1t = myContext.getAssets().open("MyFile_in_the_root.db3");

Upvotes: 5

Related Questions