I have an Android App which contains an external dependend jar library. This external library contains some png-bitmaps which I want to show on some ImageViews in my application.
Looking inside the apk file of my application I can find the images in a sub directory ("aq\img") as expected.
What is the best way to show an image (insight the classpath) on a ImageView.
I tried this
ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);
Drawable d = Drawable.createFromPath("aq/img/sample.png");
imgView01.setImageDrawable(d);
and this
InputStream is = ClassLoader
.getSystemResourceAsStream("sample.png");
Bitmap bm = BitmapFactory.decodeStream(is);
imgView01.setImageBitmap(bm);
but it did not work (I did not really expect it to be that simple). The image remains empty.
Thanks for help!
Regards Klaus
The PNG image is not a "system resource" because a system resource on Android is a file in the regular filesystem. The image that you want is within the APK archive. You would be able to use getSystemResourceAsStream
to open an InputStream
for reading the bytes of the APK archive, but not a file within the archive unless you implemented the logic to extract the file yourself.
Simply use this:
InputStream is = getClass().getClassLoader().getResourceAsStream("aq/img/sample.png");
EDIT: Note that on Android, the name that is passed to getSystemResourceAsStream
is treated as a path to the file relative to root (/
). Because the APKs are stored in /data/app
, then to open an InputStream
for reading the APK, you can use ClassLoader.getSystemResourceAsStream("data/app/PACKAGE-1.apk")
, where "PACKAGE" is the package of your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With