I'm trying to open a file with this:
document = builder.parse(new File("Data.xml"));
and I'm getting this message:
/Data.xml: open failed: ENOENT (No such file or directory)
and the file is in the root directory of the android project.
You are trying to open a file located in / (in linux this is the root directory of your file system). Instead you should be trying to create a file either on the SDCard or within the local storage directory for your application.
See this for more clarification: http://developer.android.com/guide/topics/data/data-storage.html
Move Data.xml into the assets folder of your project. Then to get a file reference, call getResources().getAssets().openFd( "Data.xml" )
You should probably try using a file input stream constructor for the builder instead, and use openFileInput( String fileName ) to get that, which does only use your app's data directory.
Using persistent storage
openFileInput()
Related
I try to access the file I had downloaded from flutter app and save it to external storage. I call the file and use it in java part but it show this error:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/abc.xyz.qwe/files/mobilenet.tflite
I have tried to grant permission before call the file but it still not work (The file is exist, i have checked).
You start the file name with a slash. What directory is the file located in? If the storage folder is not in the root directory (/), then you won't be able to find the file. If storage is in the same place as the java file you're running, you should remove the first /
I have an existing .json file in my assets folder. I can read successfully using Input steam but when I want to write to this file I'm having trouble finding it correctly. Here's how I have it now
try (FileWriter file =
new FileWriter(getApplicationContext().getPackageName() +
"/app/src/main/assets/inventory.json")) {
file.write(jsonObj.toString());
}
Here's the error I'm getting
01-09 14:43:51.178 21391-21391/edu.wit.senderp.inventorytrack W/System.err: java.io.FileNotFoundException: edu.wit.senderp.inventorytrack/app/src/main/assets/inventory.json (No such file or directory
Files in assets folder are read-only. And you can only access assets files via AssetManager.
https://developer.android.com/guide/topics/resources/providing-resources#ResourceTypes
Using react native (0.44), react-native-fs (2.3.2) and react-native-zip. Trying to create a zip and then move it to another folder.
Expected:
Create zip file of files at source folder to target path
When success (promise resolves), move zip to another location
Actual:
Zip file creation succeeds:
Successfully created zip at /storage/emulated/0/Android/data/my_app/files/2017-06-07-14_09_39.zip
After zip creation promise resolves, check if source path exists:
fs.exists('/storage/emulated/0/Android/data/my_app/files/2017-06-07-14_09_39.zip')
-> true
But:
fs.moveFile('/storage/emulated/0/Android/data/my_app/files/2017-06-07-14_09_39.zip', targetPath)
-> Error: ENOENT: no such file or directory, open '/storage/emulated/0/Android/data/my_app/files/2017-06-07-14_09_39.zip'
Read and write permissions are OK in AndroidManifest etc.
Any ideas?
Check that "targetPath" includes both target directory path and target filename. Also check that the target directory exists.
One solution could be you forgot to add the filename so the destPath also, it's not enough to specify a directory.
How do I delete a file or folder on the internal storage in android with Delphi for android
You should use the TFile and TDirectory classes in the System.IOUtils unit.
For example:
TDirectory.Delete(<YOUR DIR PATH>);
or
TFile.Delete(<YOUR FILE PATH>);
Look in Embarcadero's documentation to get the right path of your files and folders on the various platforms:
Standard RTL Path Functions across the Supported Target Platforms
Referred from :
https://stackoverflow.com/a/27047502/5193608
https://stackoverflow.com/a/3554949/5193608
Main Part from both links:
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following: All files you saved on internal storage All files you saved on external storage using getExternalFilesDir(). However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.
Source : http://developer.android.com/training/basics/data-storage/files.html
Directly you can do is :
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
Hope,it helps!
My application has a function of creating an xls file. After that I need to access that file manually. Now, the problem is, when i run the app on my device, I am able to locate the directories and my xls file in internal storage.
But when I try to run that in the AVD, I am not able to find the file. I also viewed file explorer but there also, I wasn't able to find my file.
Where does the file exists when created in AVD?
Below is the code :
File myDir = new File(Environment.getExternalStorageDirectory()+ "/PB/automation/mydir/");
myFolder.mkdirs();
File file = new File(myFolder, "demo.xls");
Trust me, there is nothing wrong with the code.
Below is the file explorer view :