Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's the difference between getResourceAsStream and getAssets(), which method should I use more?

InputStream in = getClass().getResourceAsStream("/assets/file.txt");
InputStream in_2 = getAssets().open("file.txt");

I would like to know the difference between these two methods. Will getAssets() perform better for android?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

getAssets() is recommended for use in Android development whenever you’re looking to access files in the assets directory. That method is optimized for Android and ensures better performance and integration with the Android platform. As for getClass().getResourceAsStream in this case it looks like this is being used to access resources that are part of the class path but generally speaking the getResourceAsStream method is more common in Java applications. Here’s some resources https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResourceAsStream-java.lang.String-
https://developer.android.com/reference/android/content/res/AssetManager

I hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading