I want to read a file that is saved in my java folder (same where my activities are) in package com.example so it's java-->com.example--> data.ser. However I'm getting java.io.FileNotFoundException: /java/com/example/data.ser: open failed: ENOENT (No such file or directory) when I call
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("java/com/example/data.ser")));. I tried removing the java part or just leaving the file name on its own but nothing works. I am not loading it from external/internal storage but just from within the app. How to do it? I'm using android studio. Thanks for help
Files has to be stored on 'assets' or 'raw' folder, depending on what kind of file you are going to store. On 'java' you should set code class only. You can find an easy example here:
raw file example
Related
How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)
I am probably doing something stupid here but my app cannot read data from the external storage. But i can write files just fine. In fact, i pull the files from an FTP server and write them to external storage which works fine. But as soon as I try to read them with:
getApplicationContext().openFileInput(files[i]);
I get java.io.FileNotFoundException: /data/data/co.za.infestation.opendesign.app/files/event1.txt: open failed: ENOENT (No such file or directory)
Which is weird because I know the files are there. And i know files[i] has the correct file name because I printed them before after:
String[] files = getExternalFilesDir(null).list();
Yes I did add the permissions in the manifest file... And yes, i did hardcode the file names without success..Maybe i need more coffee.
Thanks!
openFileInput()
is for opening files which reside in private internal memory. You can see the error path:
/data/data/co.za.infestation.............
Try to open with a
FileInputStream()
instead.
I have a audio file stored in a folder within the project and I'm trying to call the sound file within the folder.
I am able to call the file using the full URL
new java.net.URL("file:C:\NetBeansProjects\Puzzle\audio\applause2.wav"));
But is there a way to shorten it so that if I move the project I don't require changing the code?
Thanks
File file = new File("audio\applause2.wav");
Assuming you keep the audio folder at the same level relative to the jar
I am looking for an appropriate way to store and access pdf files in my Android app.
The problem is: I have 5 pdf-files which I would like to store in the project resources and load in the app.
I have tried to make a new directory in the project root /myApp/pdfs, and accessing it by:
File myFile = new File("/myApp/pdfs/,fileName);
I have tried some different variants of the path name: ./myApp/pdfs, myApp/pdfs, /pdfs, ./pdfs. However I get the same message stating that the file can't be found.
How do I get the path to my apps directory? And is this the most appropriate approach for saving a small number of pdf-files?
If you are wanting to load the pdfs from the app (while the app is running), you probably want to store them in the res/raw folder.
I want to acess class file from bin folder in android.
I was doing it using
File f = new File("/bin/filename.class");
Its working fine in java but in android giving path doesnt work, so
please suggest me other way to access class file of any java file in android.
You can't access file like
File f = new File("/bin/filename.class");
from bin folder. Android can't recognize this path.
Java .class files are converted to Dalvik Executable (.dex), in your apk there won't be any .class files, just one single dex file called classes.dex.
I think this post from Android Developers blog might help you: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html