#Override
public void create() {
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/image.png"));
sprite = new Sprite(texture);
}
Heres the main code to it. I believe I have linked the Android and the Desktop Assets folder but it still give me the error. I must have done something wrong but I don't know what. The image is located in C:\Users\Dakota Pederson\Desktop\Slingshot Steve\android\assets
Below is the full error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: data/image.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.dakotapederson.slingshotsteve.SlingshotSteve.create(SlingshotSteve.java:18)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: data\image.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:220)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
... 8 more
Try Gdx.files.local() to load your file, instead of Gdx.files.internal(), as the "internal" files are read out of the (compressed) APK and may not be "visible" like a normal file. (This depends a lot on what the code you're passing a pathname to wants to do with it.)
For a libGDX FileHandle file object use file.file() to pass a File handle, instead of file.path() to pass a String that might get misinterpreted.
For a libGDX FileHandle file use file.read() to pass an InputStream directly to the consumer (instead of passing a string or file handle and expecting them to open it).
A libGDX "internal" file maps to an Android AssetManager file, so they can be a bit wonky in some cases. Specifically, they may be compressed and stored within a .jar or .apk and not visible to traditional file reading functions.
Related
Im currently trying to save some values in a text file in Processing Android (APDE). I want to later use this in another context, so it's important to use a complete file path. From Processing documentation for loadStrings():
... Alternatively, the file maybe be loaded from anywhere on the local
computer using an absolute path (something that starts with / on Unix
and Linux, or a drive letter on Windows)
So it must be possible.
I already searched for a answer, but never found something for Processing.
So my code is:
String[] saveData;
int score;
void setup(){
saveData=loadStrings("/storage/emulated/0/dataP/hi.txt");
score=parseInt(saveData[0]);
fullScreen();
frameRate(60);
noStroke();
noSmooth();
textAlign(CENTER);
textSize(height/20);
}
void draw(){
background(0);
fill(255) ;
text(score, width/2,height/2);
}
void mousePressed(){
score--;
saveData[0]=str(score);
println(saveData[0]);
saveStrings("/storage/emulated/0/hi.txt" ,saveData);
}
and I get the following error:
java.lang.IllegalArgumentException: File
/storage/emulated/0/dataP/hi.txt contains a path separator
I believe the confusion stems from the fact that loadStrings() method works differently for Java mode and Android mode. In Java mode, it is definitely possible to give loadStrings() an absolute Path with included separators, but in Android mode, loadStrings() will only work if you only specify a name without any separator (assumes by default to be looking into the data folder). Therefore, having any separator inside loadStrings() will throw the error.
One simple workaround you can try is to first create a separate path variable:
String path = "/storage/emulated/0/dataP/hi.txt";
And then give that as parameter to the loadStrings() method:
saveData = loadStrings(path);
If you were to use an SD card for storage, for example, you could do something like:
String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(SDCARD + File.separator + "mytext.txt");
String[] s = loadStrings(file.getPath());
As explained in the link in the comment I posted, loadStrings() and saveStrings() does not take absolute path as argument. What it means is that it can only access files with path "name.txt" and not "folder/name.txt". You have to do it using a FileInputStream and FileOutputStream if you must use absolute path. There are many examples of both these files on StackOverflow.
In my app I have to pass a file from assets folder to shared library.
I cannot do it with use of jni right now.
I'm using precompiled shared library in my project, in which I have hardcoded path to my file, but I'm getting error "No such file or directory".
So in my .apk file I have .so file in libs/armeabi-v7a folder and my file in /assets folder.
I have tried to do it like this:
char *cert_file = "/assets/cacert.cert";
av_strdup(cert_file);
And some other paths, but it doesn't work.
Is it possible at all?
You can simply use the AAssetManager class in C++.
Basically you need to:
During the init of you library get a pointer on: AAssetManager* assetManager
Use it to read your file:
// Open your file
AAsset* file = AAssetManager_open(assetManager, filePath, AASSET_MODE_BUFFER);
// Get the file length
size_t fileLength = AAsset_getLength(file);
// Allocate memory to read your file
char* fileContent = new char[fileLength+1];
// Read your file
AAsset_read(file, fileContent, fileLength);
// For safety you can add a 0 terminating character at the end of your file ...
fileContent[fileLength] = '\0';
// Do whatever you want with the content of the file
// Free the memoery you allocated earlier
delete [] fileContent;
You can find the official ndk documentation here.
Edit:
To get the AAssetManager object:
In a native activity, you main function as a paramater android_app* app, you just need to get it here: app->activity->assetManager
If you have a Java activity, you need so send throught JNI an instance of the java object AssetManager and then use the function AAssetManager_fromJava()
Your assets are packaged into your apk, so you can't refer to them directly during runtime like in the sample code you provided. You have 2 options here:
Process the assets as an input stream, using
Context.getAssets().open('cacert.cert')
Copy out your asset to a local file in your files dir, and then reference the filename of the copied file.
Building off of the answer by #Sistr , I used getBuffer along with AASSET_MODE_BUFFER (instead of AAsset_read).
However, I was trying to write the buffer to an ofstream. I got signal crashes using the <<operator:
ofstream myStream(<some file>, ios_base::binary);
auto buffer = AAsset_getBuffer(asset);
myStream << buffer;
I presume this is because the buffer pointer points to the asset without a NULL character at the end (I was reading a text asset; Android source code). Using the write function worked:
ofstream myStream(<some file>, ios_base::binary);
auto buffer = AAsset_getBuffer(asset);
myStream.write((char *)buffer, AAsset_getLength(asset));
This is because the <<operator does not have to call strlen to find out how many characters to write since it is explicitly given by AAsset_getLength.
I'm currently writing a LibGDX game and I need to open an XML file for a spritesheet. Unfortunately, when I try to open the file, I get an IOException. The files exist in the right places, the project's cleaned and up to date, etc.
Here's the kicker: LibGDX will open and display image files in the same directory, and in fact will obtain the XML file in its own FileHandle object (I've had it logcat the size and it matches up). Yet when I send the file off to a SAX parser, I get an exception leading me back to the file saying that it can't open it.
Here's the problem line of code inside the ClipSprite class:
private void parseConfigFile(FileHandle file) throws ParserConfigurationException, SAXException
{
//Use a SAX parser to get the data out of the XML file.
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLConfigHandler handler = new XMLConfigHandler();
//Parse through the document
try
{
parser.parse(file.path(),handler); //IOException here
}
And here is the code where I select the file in the game in the create method in the main LibGDX class
//This loads a png file
texture = new Texture(Gdx.files.internal("data/graphics/CDE1/CDE1.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
//Here I load the xml file and it tells me how many bytes it is
Gdx.app.error("File","xml bytes:"+Gdx.files.internal("data/graphics/CDE1/CDE1.xml").length());
//Here I load the same file and it crashes
//Note that it looks in the subdirectory and finds /CDE1.xml properly
animation = new ClipSprite("data/graphics/CDE1");
Error from logcat:
java.io.IOException: Couldn't open data/graphics/CDE1/CDE1.xml
Why can it identify the file, yet I still get IOExceptions about being unable to open it? Thanks for the help, and I'll provide any more information if necessary.
Just to coalesce all the comments above into a single answer in case someone else runs into this or something similar:
Try Gdx.files.local() to load your file, instead of Gdx.files.internal(), as the "internal" files are read out of the (compressed) APK and may not be "visible" like a normal file. (This depends a lot on what the code you're passing a pathname to wants to do with it.)
For a libGDX FileHandle file object use file.file() to pass a File handle, instead of file.path() to pass a String that might get misinterpreted.
For a libGDX FileHandle file use file.read() to pass an InputStream directly to the consumer (instead of passing a string or file handle and expecting them to open it).
A libGDX "internal" file maps to an Android AssetManager file, so they can be a bit wonky in some cases. Specifically, they may be compressed and stored within a .jar or .apk and not visible to traditional file reading functions.
Good afternoon everyone,
I'm having a problem here with an CSV file that I wanna use to fill a dynamic table in Android.
I placed the file in the "data" folder of my emulator, and I did the following declaration to create my file variable:
file = new File("/data/Motors.csv");
I used a CSVReader class posted on another thread from this page (Link to the class) and also declared the following line to do the parse:
readerCSV = new CSVReader(new FileReader(file),';','"',0);
In the last line, the programmer to be able to create the variable has to send as parameters a Reader (Or a FileReader, it doesn't make a difference), the separator char, the quote char and the number of how many lines the CSV reader has to skip.
The problem comes that no matter in which directory (Not even in the root directory, which Java tells me that it's "/" through the file.getAbsolutePath(); method) it always gets to the point when the program throws the FileNotFoundException and I'm getting a bit frustrated because I have 2 days already with this problem.
If someone could please help me to get a bit of orientation of what I should do to get the file from the right directory... It would be a lot of help and I can go further with the code I have to finish.
Many thanks in advance!!!!
EDIT
I found a solution to open my file:
I have used 2 file variables: one gets the rout of the External Storage Directory (see first answer in the following link) and the other is declared as a new file, like the following lines in the code:
route = Environment.getExternalStorageDirectory();
file = new File(route,"Motors.csv");
Later I used again the CSV reader class that I found, where the first parameter is from the FileReader type (Sorry #Rajesh, but I couldn't use your alternative because you passed a FileDescription parameter from the AssetManager to the CSV reader class and then I had to change the whole class and that didn't help my problem at all).
Now I don't get the exception at all!!
There are various Storage Options in Android. Files can be stored in internal and external storages. If the CSV file is a static resource, you could think of bundling the file in the "assets" folder of your project.
If you are using the "assets" route, the following code segment can get you a CSVReader.
AssetManager am = getAssets();
AssetFileDescriptor afd = am.openFd ("Motors.csv");
FileDescriptor fd = afd.getFileDescriptor();
readerCSV = new CSVReader(fd, ';','"',0);
The above code snippet doesn't perform error/exception handling, please remember to take care of that. You need to copy the Motors.csv file to the "assets" folder within the project.
I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.