Reputation: 4589
This is how my asstes folder in android looks like. I am trying to get the path to the "19.png" inside assets. I will use this path in file creation as in new File("pathTo19.png")
. I tried variations of "android/assets/19.png"
, "assets/19.png"
etc but none worked. What is the path to the assets folder in libgdx project?
EDIT: I'm gonna use it in a MMS. Based on @Majid Daei Nejad answer I tried this. However, inside mms there is no image, just text and template image(not 19.png) so I'm guessing that the path is incorrect. The file path printed out is "file:/data/19.png"
.
FileHandle fh = Gdx.files.internal("data/19.png");
Intent sendIntent1 = new Intent(Intent.ACTION_SEND);
try {
sendIntent1.setType("text/x-vcard");
sendIntent1.putExtra("address","0475223091");
sendIntent1.putExtra("sms_body","hello..");
sendIntent1.putExtra(Intent.EXTRA_STREAM,
Uri.parse(fh.file().toURI().toURL().toString()));
System.out.println(fh.file().toURI().toURL().toString());
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.startActivity(sendIntent1);
Upvotes: 1
Views: 1921
Reputation: 1037
Simply It depends on how do you want to use android assets files. If you want add it in a Texture use this method:
Texture texture = new Texture("19.png")
If you want to use files in assets folder for other uses like audio or freeType, you can use this:
FileHandle fh = Gdx.files.internal("data/my_music.mp3");
As you can see it point to data
folder.
EDIT: Ok! It seems that your problem is not related to libgdx, only in an Android project. So please remove libgdx tag. And for your problem , the only way to share an image in Assets folder is using ContentProvider that has been asked in this issue: android share images from assets folder
Upvotes: 0
Reputation: 4589
This is working. All hail Tenfour04!
FileHandle from = Gdx.files.internal("19.png");
from.copyTo(Gdx.files.external("19.png"));
FileHandle ext = Gdx.files.external("19.png");
Intent sendIntent1 = new Intent(Intent.ACTION_SEND);
try {
sendIntent1.setType("image/png");
sendIntent1.putExtra("address","0475223091");
sendIntent1.putExtra("sms_body","hello..");
sendIntent1.putExtra(Intent.EXTRA_STREAM,
Uri.parse(ext.file().toURI().toURL().toString()));
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.startActivity(sendIntent1);
Upvotes: 2
Reputation: 93609
The uri for the assets directory is
file:///android_asset/
Libgdx doesn't return absolute paths for internal, external, or local Android files (because Android doesn't).
Upvotes: 0
Reputation: 103
Well, i don't know if this will help but, what you can try is getting the location of your main class and then adding the name and type of that file you want. You will have to place the image in the same package as your main class. Create a new class called loadImageFrom (or whatever you want) and add this code in it:
public static BufferedImage LoadImageFrom(Class<?> classfile, String path) {
URL url = classfile.getResource(path);
BufferedImage img = null;
try{
img = ImageIO.read(url);
}catch(IOException e) {
e.printStackTrace();
}
return img;
}
Then get the image by:
loadImageFrom.LoadImageFrom(YourMainClassNameHere.class, "YourImageNameHere.YourImageTypeHere")
For example:
g.drawImage(loadImageFrom.LoadImageFrom(Main.class, "playersheet.png"), locationX, locationY, width, height, imageobserver);
If this didn't help, i'm sorry. This came from my PC game code but i think it would work for android too.
(This works for anything: textures, just HUD, GUI, anything basicly)
Upvotes: 0