Path for getResourceAsStream in Android Project - java

I am trying to programmatically load a .png file but I am not sure what is the exact path I should be giving. I tried different paths that I think would get me to the resource but all return null:
InputStream is = getClass().getClassLoader().getResourceAsStream("playstore.png");
Bitmap bmp = BitmapFactory.decodeStream(is);
mStoreImage.setImageBitmap(bmp);
If I understand correctly I cannot use resources as I am building a .jar. This is my project package structure displayed with Android Studio:

You should use Resources.openRawResource()
e.g. if your Activity
getResources().openRawResource(R.raw.playstore.png);
remember to put the file under res/raw
Edit:
In addition, android has it own method to get drawable (e.g. a png file) into Bitmap

Android Studio Gradle projects read files from a separate resources directory in the same way as Maven does it.
Create a "resources" folder in Windows Explorer (Android Studio don't allow you to do that) and then recreate the package path and place files there, e.g.:
A class located at this path:
C:\...\app\src\main\java\com\adscendmedia\videosdk\AdscnedAPI.java
Read resource files from this path:
C:\...\app\src\main\resources\com\adscendmedia\videosdk\playstore.png

Related

Making folders of images available in the project

I need set of images in jpg format to be in this specific folder.
path = getFilesDir() + "/imagefiles/";
After root my mobile phone, I manage to find the specific folder. The location is /data/data/*packagename/files/imagesfile/*png images.
I can copy the image manually into the folder but i want to deploy a project that already has the image in it.
I have tried several solution but none really works for me. Please help me.
I can copy the image manually into the folder but i want to deploy a project that already has the image in it
It is not possible for you to create an app that automatically has files in getFilesDir() immediately upon installation.
What is possible is for you to put images there on the first run of your app. Those images could be:
downloaded from the Internet
packaged in assets/ in the project, then copied into your desired location using AssetManager and open()
packaged as raw resources in the project, then copied into your desired location using Resources and openRawResource()
etc.

Java won't export my resources properly

So I have 2 class folders one is res the other is lib. My res folder has two other sub folders one with images the other with sounds. My lib folder has 4 other jar files and a native folder. It all works within eclipse but when I try to export it as a runnable jar it does not work. I won't won't recognize anything.
I am to call my images I am using ImageIO.read(new File(imagePath)); For the sound I am using the external libraries I mentioned earlier to load and play them.
I am to call my images I am using ImageIO.read(new File(imagePath))
Contrary to your title, this is not an Eclipse problem - it's simply a bug in your code, because your code assumes that the image is stored as a file in the file system, when it's not.
You don't have a file for the image, so you shouldn't use new File. You should instead use Class.getResource or ClassLoader.getResource - or the getResourceAsStream equivalents. That way, it will load the resource from whatever context the class itself is loaded, which is appropriate for jar files. So for example, you might have:
Image image = ImageIO.read(MyClass.getResource("foo.png"));
... where foo.png is effectively in the same package structure as the class. Alternatively:
Image image = ImageIO.read(MyClass.getResource("/images/foo/bar.png"));
where images is a folder within the root directory of one of your jar files loaded by the same ClassLoader. (We don't have enough information to give you complete code here, but that should be enough to get you going.)

Find a resource both in .jar and eclipse with the same String computation

I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.

Android FileNotFound Exception with a plist file that will be built in

I am trying to open a file for an Android app I am making. I tested the code that opens this file on a regular Java project, and it opens fine. However when I use this code in an Android Java project I get an java.io.FileNotFoundException error.
File file = new File ("list.plist");
Now the list.plist file is in the parent directory of the project. This file will be included in the app as its the only one it will be using.
I guess I am used to Xcode where I can just place the file anywhere in the project and I can access it without a problem.
How am I supposed to structure this path?
Thanks :-)
I've used this but it also did not work.
File file = new File(context.getFilesDir().getAbsoluteFile(), "list.plist");
Firstly, you need to ensure the file you are using is packaged within your app - such as in the assets directory.
Secondly, Android automatically compresses plist files based on the extension. As a workaround, change the extension to something Android will not compress. These extensions include:
".jpg", ".jpeg", ".png", ".gif",
".wav", ".mp2", ".mp3", ".ogg", ".aac",
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
".amr", ".awb", ".wma", ".wmv"
(for full article, see dealing-with-asset-compression-in-android-apps)
You need to put this file in the assets dir and not in the parent folder. Any files in the parent folder need not be packaged with the app. Moreover, the structure of your files on a phone is not the same as you see in eclipse.
You should use "res/raw" or "assets" directory to include a raw file in your Android project.
see: http://developer.android.com/guide/topics/data/data-storage.html
I added the list.plist to the assets folder and renamed it list.mp3 to avoid compression issues.
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("list.mp3");
This successfully opened the file without problems, which now allows me to parse the inputStream.
Thanks to Phil for giving sending me in the right direction.

Deploying .jar file: Why can't I load icon files?

I'm exporting a simple java project that includes two directories; src and Icons. Icons is a directory that contains three .png files.
I'm exporting to an executable .jar file using File -> Export. The export works properly and the .jar file contains the Icon directory. But I can't get the correct path for the .png files when the project is deployed. During the development I'm using the following path:
Icons/picture.png
and it works as long as I run from within the Eclipse IDE.
How do I get the correct path for the icons?
Your code is looking for the image outside of the .jar file. Try the URL constructor of ImageIcon instead.
Icon icon = new ImageIcon(getClass().getResource("Icons/picture.png"));
See Class.getResource().
mmyers is correct, but be aware that getClass().getResource() will load resources relative to the package where the class is defined. I suspect your icons are packaged at the root of the jar file and not relative to the class itself. To get resources from the root of the classpath, try:
getClass().getClassLoader().getResourceAsStream("Icons/picture.png")

Categories