File not found on Android Device - java

I have an error here:
FileHandle file = Gdx.files.internal("hscore.json");
Showing that it can't be found:
...
com.badlogic.gdx.utils.GdxRuntimeException: File not found: hscore.json (Local)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
...
It works on my Desktop, but fails like this on my Android Phone.
I use Android Studio, where I keep the file in the assets folder which is in the android folder.
I have no idea why it can't find it on the android?
Update: I don't know what happened in meantime, but now it highlights this line of code after throwing the same error:
String s = file.readString();
It can read .txt files and other files I'm using just fine, seems the .json file is the problem for android?
Until this is resolved, I'm using Preferences to save data there, rather than the whole class in .json

Thanks to a chat with p.streef This question has come to a closure, sorta.
1) I created a new test.txt file and it worked fine reading that.
Maybe then the .json file itself was a real issue?
2) But then I renamed it to text.json, and afterwards back to hscore.json
It worked fine in both cases when opening and reading the file.
I had no idea why the initial file .json was not readable?
3) I also killed and restarted my android studio in meantime because it froze at a point when building my project.
After all of this the problem of file not being found was resolved by itself.
Anyway then I realized that the file is not writable since internal files are read-only.
I decided to stick then with the Preferences as the final solution.

Related

Why am I getting parsing error when opening APK file from app cache?

I'm trying to download an APK file online from within my app and then install it using Java. My code basically, downloads the file to the device from my server and then uses an Intent to open the installation message from the package manager and allow the app to be installed.
It works perfectly for me... unless I save the file to cache instead of regular storage. If I save it to cache instead, I get an error that it could not parse the APK file.
I checked and it is downloading the entire file to the cache correctly, but it just not installing. Any ideas?
The only difference in my code is that instead of using Environment.getExternalStorageDirectory() + "/file.apk" I'm trying to use context.getCacheDir() + "/file.apk" as for my file path.
I want to put the file in the cache so that Android can handle the cleaning of the file and so that I don't need to worry about handling write permissions on different Android versions and the variations of dealing with external storage.

Where to find the created file in Android emulator in Windows

I created a simple file write/read app in Eclipse and have successfully tested that it could read what it wrote. The structure is fairly simple. I use FileOutputStream to write a samplefile.txt using openFileOutput and a FileInputStream using openFileInput to read it back. The only minor thing is that I don't know where is the file on my physical hard drive? Can anybody point me to where I could find the file in Windows?
If you dont give a absolute path the file will be creataed in aplication directory.
This is /data/data/packge-name-of-your-app/
e.g:
/data/data/com.example.test/
See more http://developer.android.com/training/basics/data-storage/files.html
Go to DDMS perspective, select the emulator which you are using, go to package explorer tag ,then you will get a list of folders hierarchy.
Go to /data/data/
Now you will see a list of packages installed, search for the package name of your app and expand it.
Here you will see the files which your application created when it was running.
P.S : You cannot find this file on the physical drive of your computer,because it is present in the storage of the emulated android virtual device. This storage cannot be directly accessed by you.

eclipse java can not find xml file

i am currently having a problem regarding an xml file while programming in eclipse android.
the problem is that it seems to ask for a folder called R within my project but such a folder was not created on creation of the project.
i have had a look on the internet and review other questions but i cant quite seems to find a solution.
i am trying to set an xml file for a list and this is all the code that should be needed but it can not find the .xml file at all as it seems to want a folder called R to contain it.
new ArrayAdapter<Object>(this,R.layout.edit_item_layout , theList);
anyhelp and suggestion will be well received
UPDATE of useful information
The weird part is it can find activity_main.xml which is in the same folder
the file is located in res/layout
ok i cleaned and now it is giving a list of files i can change it to but the file itself doesn't seem to be in the R.java file ad it is asking me to change it to different files but not the one i want
R is automatically generated, and you should never add or delete from this file.
I have faced this problem many times, and it mostly caused by xml file.
You should check your XML file carefully. It is most likely that you will find your answer there.
If your importing any library, make sure you're importing those file correctly.
R refers your res folder. You should have a subfolder in res called layout that contains your edit_item_layout.xml.
If you're receiving the error R cannot be resolved to a variable, you'll need to:
import your_package_name.R;
If you're receiving the error edit_item_layout cannot be resolved or is not a field, try:
Cleaning your project by selecting: Project --> Clean
Right clicking your project and selecting: Refresh
If you're R.java file is corrupted, because it's automatically generated you can also delete your gen folder and it should be replaced immediately.

Access external files (Outside of AVD device) using Android JUnit

I am using Robotium to automate Android application. Created a android test project for the same. Inside project folder I want to keep some files like .properties/.xls etc. and want to read from /write to those files. For example, I have one config.properties file under my Android test project directory (src/main/java/config) and I want to access that file through coding:
For normal Java project I used following code snippet to load config.properties file:
CONFIG = new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/config/config.properties");
CONFIG.load(fs);
This same code - when executed as Android JUnit project throwing error saying java.io.FileNotFoundException: //src/main/java/config/config.properties: open failed: ENOENT (No such file or directory) and unable to locate the file.
If anyone faced this anytime before please help me to get through.
Thanks
Sitam Jana
Software QA Enginner
Mindfire Solutions
You cannot access the internal structure of your java project that way. When you install your apk on the device, your java code gets converted to dex code for the Dalvik Virtual Machine, and you won't have the file structure from your project.
You can try the following:
If you only want to read the config.properties you should use the http://developer.android.com/reference/android/content/res/AssetManager.html class, and put your config.properties in /assets/ in your project.
If you also want to write your .properties file, consider creating a temporary file like this: Creating temporary files in Android.
Best Regards.

Can't find folder or file created with Android App with windows file exlorer

I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.

Categories