9

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)
Community
  • 1
  • 1
Arulnadhan
  • 923
  • 4
  • 17
  • 46

3 Answers3

3

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

Community
  • 1
  • 1
morpheus05
  • 4,772
  • 2
  • 32
  • 47
2

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

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
0

Please check following image.
Add Assets

When you are going to use it in code, you can use following code
// for image in Activity
   AssetManager assetManager = getAssets();
   InputStream instr = assetManager.open("close_btn.png");
   BitmapFactory.Options options = new BitmapFactory.Options();
   mImage = BitmapFactory.decodeStream(instr, null, options);

Ken W.
  • 1,550
  • 1
  • 14
  • 18