Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding images to a Java project in Android Studio?

I'm trying to add my java project in android studio. I got many references & added my Project in Android studio.

Now i am unable to add the assets to my projects. So please help me to solve it

Thanks in Advance

Structure

enter image description here

CODE

    this.trayIcon = new TrayIcon(ImageIO.read(this.getClass().getResourceAsStream("icon.png")));
    this.trayIcon.setImageAutoSize(true);
    this.trayIcon.setToolTip("Remot Server");
    this.trayIcon.setPopupMenu(menu);

    SystemTray.getSystemTray().add(this.trayIcon);

Error

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1348)
like image 324
user3467240 Avatar asked Dec 18 '15 21:12

user3467240


2 Answers

If you refresh your project in the gradle view (the blue arrow) and AS mark it as a resource folder you should be covered. The other thing is the path: Your path should start with a slash / when using Class.getResource() because your image is in the root of the resource folder.

See the acccepted answer here: Classpath resource within jar

like image 84
morpheus05 Avatar answered Sep 22 '22 13:09

morpheus05


You resource directory is in a non-Android Studio standard location. My guess is that you haven't altered your Gradle file to try to include that directory.

Best would be to move the image assets into the standard location, so that the apk packaging routines know what to put where. While its a pain for existing projects the process is straight forward:

  1. Create a new empty Android Studio generated app so that all of the configuration is up to date
  2. Copy over the code and resources into the appropriate place.

Alternatively

You could try to add your resources from your Java project in your build.gradle (app):

android {
    ....
    sourceSets {
        main {
            // default is resources.srcDirs = ['src']
            resources.srcDirs = ['src/main/java']
        }
    }
 }

But I haven't tried it with images.

More details here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Project-Structure

like image 29
Morrison Chang Avatar answered Sep 21 '22 13:09

Morrison Chang