Skip to content

Tag: resource

Android: acessing internal resoures

a new android I just drew. source-code: android_look.svg. CC-BY-SA as usual.

You can acess internal Android resources such strings, drawables, layouts and others. For example, if you need to create a button with the text “Cancel” you can do:

Using this you are using the internal resource for “Cancel” in that Android and all its i18n. Using the same logic you can access drawables, layouts, etc.

JavaFX, Acessando Recursos de Dentro do JAR

us flagTranslation: there’s a English version of this article.

Para algumas classes como o javafx.scene.image.Image é fácil abrir imagens de uma localidade remotada com:

ImageView {
    image: Image {
        url: "http://example.com/minhaFigura.png"
    }
}

ou uma imagem local com a constante __DIR:

ImageView {
    image: Image {
        url: "{__DIR__}/minhaFigura.png"
    }
}

Mas para outras classes abrir recursos internos (de dentro do próprio arquivo jar) não é tão direto. Por exemplo, no artigo Parsing a XML Sandwich with JavaFX eu tive que colocar o arquvio XML dentro de um diretório temporário. Uma maneira mais elegante teria sido:

package handlexml;

import java.io.FileInputStream;
import javafx.data.pull.*;
import javafx.ext.swing.*;
import javafx.scene.Scene;
import javafx.stage.Stage;

class Resource{
    function getUrl(name:String){
        return this.getClass().getResource(name);
    }

    function getStream(name:String){
        return this.getClass().getResourceAsStream(name);
    }
}

var list = SwingList { width: 600, height: 300}

var myparser = PullParser {
    documentType: PullParser.XML;
    onEvent: function (e: Event) {
        var item = SwingListItem {text: "event {e}"};
        insert item into list.items;
    }
    input: Resource{}.getStream("my.xml");
}
myparser.parse();

Stage {
    title: "Map"
    scene: Scene {
        content: list
    }
}

Com um simples arquivo XML chamadovmy.xml dentro do seu pacote.



   
   
   
   
   

fileplace

E temos os mesmos resultados de antes, mas com todos seus aquivos dentro de seus Jars.

Referencias:

JavaFX, getting resources of inside your JAR

br flagTradução: há uma versão em Português desse artigo.

For some classes like javafx.scene.image.Image is easy load an image from a external resource like:

ImageView {
    image: Image {
        url: "http://example.com/myPicture.png"
    }
}

or a resource inside your own Jar file with the __DIR__ constant:

ImageView {
    image: Image {
        url: "{__DIR__}/myPicture.png"
    }
}

But for other classes loading a internal resource (inside your own jarfile) is not so direct. For example, in the article Parsing a XML Sandwich with JavaFX I had to place the XML file in a temp directory. A more elegant way would be:

package handlexml;

import java.io.FileInputStream;
import javafx.data.pull.*;
import javafx.ext.swing.*;
import javafx.scene.Scene;
import javafx.stage.Stage;

class Resource{
    function getUrl(name:String){
        return this.getClass().getResource(name);
    }

    function getStream(name:String){
        return this.getClass().getResourceAsStream(name);
    }
}

var list = SwingList { width: 600, height: 300}

var myparser = PullParser {
    documentType: PullParser.XML;
    onEvent: function (e: Event) {
        var item = SwingListItem {text: "event {e}"};
        insert item into list.items;
    }
    input: Resource{}.getStream("my.xml");
}
myparser.parse();

Stage {
    title: "Map"
    scene: Scene {
        content: list
    }
}

With a simple XML file called my.xml inside your package.



   
   
   
   
   

fileplace

And we get the same result as before, but all files inside our Jars.

References: