2015-12-18 65 views
9

我想在android studio中添加我的java项目。我有很多references &在Android studio中添加了我的项目。将图像添加到Android Studio中的Java项目中?

现在我无法将资产添加到我的项目。所以,请帮我解决这个问题

由于提前

结构

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); 

错误

Exception in thread "main" java.lang.IllegalArgumentException: input == null! 
    at javax.imageio.ImageIO.read(ImageIO.java:1348) 
+0

可能与:HTTP:/ /stackoverflow.com/questions/24661229/getresourceasstream-returning-null-despite-called-file-being-in-same-dir-as-clas –

+0

感谢您的链接。它不适用于我 – user3467240

+0

这是什么TrayIcon无论如何,为什么你需要如此复杂的方式与imputStreams? – RexSplode

回答

2

resource目录位于非Android Studio标准位置。我的猜测是,你还没有改变你的Gradle文件来尝试包含该目录。

最好的办法是将图片资源移动到标准位置,以便apk包装例程知道放置在哪里。虽然其现有项目的过程是简单的一个痛:

  1. 创建一个新的空的Android Studio生成应用程序,以便所有的配置是最新的
  2. 拷贝过来的代码和资源投入到适当的位置。

或者

你可以尝试从您的Java项目 在你的build.gradle(APP)添加您的资源:

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

但我还没有与图像尝试过。

更多细节在这里:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Project-Structure

+0

我试过你的解决方案。但它仍然没有工作? – user3467240

3

如果您刷新视图的gradle项目(蓝色箭头)和马克它,你应该覆盖的资源文件夹。另一件事是路径:当使用Class.getResource()时,您的路径应该以斜杠/开头,因为您的映像位于资源文件夹的根目录中。

看到这里的acccepted答案:Classpath resource within jar

0

请检查下面的图片。
Add Assets

在想要在代码使用它,则可以使用下面的代码
//用于活动图像
      AssetManager assetManager = getAssets();
      InputStream instr = assetManager.open(“close_btn。PNG“);
      BitmapFactory.Options选项=新BitmapFactory.Options();
      mImage = BitmapFactory.decodeStream(INSTR,NULL,选项);

相关问题