Get Path of Android Resource

Get path of Android resource

Use URI Paths instead of "absolute" path, see this post

Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");

Or use openRawResource(R.id) to open an inputStream, and use it the same way you would use a FileInputStream (readonly)

How can I get file path on raw resources in Android Studio?

how can i get file path on raw resources?

You can't. There is no path. It is a file on your development machine. It is not a file on the device. Instead, it is an entry in the APK file that is your app on the device.

If your library supports an InputStream instead of a filesystem path, getResources().openRawResource() on a Context to get an InputStream on your raw resource.

How to get a resource's path from the app using an Android Library?

Thanks to CommonsWare:

To be clear, the font file goes in main/assets/font/myFont.ttf in your module. You would then pass font/myFont.ttf to setTextFontName().

On my test app using SDK B, I placed myFont.ttf in app/main/src/main/assets/font and set it using sdk_b.setTextFontName("font/myFont.ttf").

Android how to get file path from /res/raw/

You can execute your bash like this:

BufferedReader r = new BufferedReader(new InputStreamReader(scriptFile));
String command = "";
List<String> commands = new ArrayList<>();
try {
command = r.readLine();
commands.add(command);
while(command != null){
command = r.readLine();
commands.add(command);
}

Process p = new ProcessBuilder().command(commands).start();
} catch (IOException e) {
e.printStackTrace();
}

And you can get the Uri of the raw resource in order to create a new File object:

Uri uri = Uri.parse("android.resource://com.your.package/raw/filename");

Get the Path of a .mp3 raw resource from URI

First, raw resources are not files on the filesystem of the device.

Second, a URI is not a file.

Use Resources and openRawResource() to get an InputStream on the resource. IIRC, there is a readBytes() extension function in Kotlin that you can use to read in the contents.

android - how to get path of mp3 stored in raw folder

Is it possible to get file path by using R.raw.mp3file

No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.

Either:

  • Do whatever you are trying to do some other way that does not involve a file path, or

  • Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of that resource to a local file, such as on internal storage

How to get absolute path to file in /resources folder of your project

You can use ClassLoader.getResource method to get the correct resource.

URL res = getClass().getClassLoader().getResource("abc.txt");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();

OR

Although this may not work all the time, a simpler solution -

You can create a File object and use getAbsolutePath method:

File file = new File("resources/abc.txt");
String absolutePath = file.getAbsolutePath();


Related Topics



Leave a reply



Submit