I'm testing my app (with android, core and desktop modules) on desktop, everything works fine.
Now that I try to run on Android, it gives me FileNotFound. I linked android asset to desktopLauncher before running on desktop, but Am I meant to do something similar for android?
File file = Gdx.files.internal("liver_initial_joints.json").file();
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
str = new String(data, "UTF-8");
I have my file at .../android/assets/liver_initial_joints.json
Android does not provide direct java.io.File access to the internal app directory. If you look at the javadocs for libgdx's FileHandle.file(), you'll see that it says it's not usable for anything but Absolute and External types of files.
If you just need to read a string or byte array, you can use fileHandle.readString() or fileHandle.readBytes(). If you need an input stream, use fileHandle.read();.
For example:
byte[] data = Gdx.files.internal("liver_initial_joints.json").read();
str = new String(data, "UTF-8");
I find a really easy way to do this is with:
InputStream is = context.getAssets().open("liver_initial_joints.json");
Related
I wrote an android application that part of it is to handle upload and download documents. Currently I am using the Microsoft Azure server to save the files on.
The way I am currently doing it is by turning the files to a string and saving it that way on the Azure server:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(Uridata.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();
item.setStringFile(Base64.encodeToString(bbytes, Base64.URL_SAFE));
item.setName(Uridata.getLastPathSegment());
where item is my class that saves the string representation and the name of the file and is being loaded to the Azure, Uridata is an Uri instance of the file chosen.
I have one main problem with this solution and it is the limit on the file size.
I am searching for a good server to use instead of the Azure (maybe a RESET one) and if there is a better way to save files of all kinds (pdf, word...).
I will also want in the future to use the same data in a web interface
Does anybody have any suggestions on how to do it?
Thanks in advance!
To start, you don't have to transform the file into a string, you can just save it as a file. You have the possibility of losing data by continuing to do that. See: How do I save a stream to a file in C#?
If you're looking for another service to save files, then you should look into Azure Blob Storage. It will allow you to upload as much data as you want to a storage service for arbitrary files. See for example:
https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/
Looking for a way to prevent users from opening a text file that i transfer from android to PC
Through DropBox.
is convert the text file to bin file Will do the job ?
can i get any sample code in java (for android) that convert text file to bin file ?
i try this but dont work:
Reading the file:
File queryImg = new File(ImagePath);
int imageLen = (int)queryImg.length();
byte [] imgData = new byte[imageLen];
FileInputStream fis = new FileInputStream(queryImg);
fis.read(imgData);
Writing the file:
FileOutputStream f = new FileOutputStream(new File("/MyPath/xx.bin"));
f.write(imgData);
f.flush();
f.close();
If I correctly understand the question, I'd suggest either changing file extension from .txt to otherwise nonexistent extension of your choice (.gldsft or something like that), encrypting, or not going with text file at all.
I have this code:
try {
URL url = new URL (strURL);
input = url.openStream();
byte[] buffer = new byte[1500];
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
and I get this error:
do not hardcode /sd card/ use environment.getexternalstoragedirectory().getpath() instead
in
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
I have read about that:Android 4.2 - Environment.getExternalStorageDirectory().getPath() behaviour
So the question is what will be final code when I replace it?
sd folder name different in Some phones
In samsung phones, it is named external_sd , and your code will fail.
control+shift+o --> to add imports in eclipse,see this link
"/sdcard/" is replace with "Environment.getExternalStorageDirectory().getPath()" in your code
The problem was that you call a file called Environment.java itself, so Eclipse didn't give me the choice to import Environment change that one.
Your revised code should look like
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+pos+".png");
You should be careful as not all devices have a /sdcard/ path.
Devices across the years have changed and this may not always contain a link to the proper external storage directory.
I'm trying to upload a file to an FTP using apache commons ftp.
I am using a code that I have seen on several websites, including stackoverflow
Android FTP Library
The problem is that in the line:
Buffin = new BufferedInputStream (new FileInputStream (file));
I can not put any paths in "file", eclipse does not validate any values or path-
What would have to indicate in "new FileInputStream"?
I do not know I'm doing wrong.
Thank you very much and best regards
You need a File object to pass it to the FileInputStream.
Buffin = new BufferedInputStream(new FileInputStream(new File("/path/to/file"));
And you can't Upload file to FTP because FTP is not a place, it is a protocol.
Create a File object with the path and pass it in.
You can do this:
Buffin = new BufferedInputStream (new FileInputStream (<path to your file>));
Details here:
FileInputStream
I've found through research on google that that I can read a text file by storing it in my res/raw folder and then accessing it through getResources().openRawResource(R.raw.words);
In a class WordHelper I the constructor provides throws an InvocationException on this line of code:
istream = getResources().openRawResource(R.raw.words);
Which is the one most examples seem to use without a problem, I will then go on to do this
isreader = new InputStreamReader(istream);
myReader = new BufferedReader(isreader);
once everything is working and then use the readLine() method.
All descriptions of InvocationException such as getCause are null, I definately have the file in the res/raw/words.txt.
Thanks for reading.
I got the same problem and solved it using isreader = new InputStreamReader(istream, "UTF8");