csv FileNotFoundException: ENOENT - android

I am trying to access a csv file from the assets for my app and all I get is FileNotFoundException. I get the same when I try to access it from elsewhere. Any idea what I should be doing here?
Log messages work just fine until then. The error on the logcat is as follows.
Tag Text
System.err java.io.FileNotFoundException C:\Users.....\assets\cms.csv: open failed ENOENT (no such file or directory)
for the code:
br = new BufferedReader(new FileReader
("C:\\Users\\Srihari\\workspace\\CMSHealthcare\\assets\\cms.csv"));
Any help is appreciated!
Thanks in advance!

Dont point it to the file system on your computer...instead use the getAssets method to target the filesytem in the raw folder that gets bundled into your apk (the binary of the app)
br = new BufferedReader(new InputStreamReader(getAssets().open("cms.csv")));

Related

FileInputStream can't read file in Android R

File file = new File(filePath);
InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
simple piece of code.
i checked the following:
file!=null
file.isFile()
file.exists()
file.canRead()
everything is true. The program have the permission and can actually read this existing file, but put it in a FileInputStream, it throws the following exception:
java.io.FileNotFoundException: /storage/emulated/0/XNR/Setting.gson: open failed: EFAULT (Bad address)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileInputStream.<init>(FileInputStream.java:159)
...
Caused by: android.system.ErrnoException: open failed: EFAULT (Bad address)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7372)
at libcore.io.IoBridge.open(IoBridge.java:478)
at java.io.FileInputStream.<init>(FileInputStream.java:159) 
... 
it works well only on a api 23 emulator now. maybe the issue is cause by some new mechanics of newer version android api?
funny thing is, i developed and tested it on a android r emulator exclusively 2 months ago and everything is fine, after tested on several actuall devices, the project was deemed done and finished, it sat in my hard drive untouched for 2 months, i can even see the same .gson file generated by it. almost looks like it just went bad on it's own.
Using getFilesDir() or getExternalFilesDir(String) to access storage instead of using Environment.getExternalStorageDirectory() solved the problem, android no longer supports that since q based on the comment above.

How to load an SSL certificate using an input stream for Retrofit

I need to add my SSL certificate for Retrofit in Android but I can't find the file using Google's example code for doing this.
Here's the example from Google that I'm using https://developer.android.com/training/articles/security-ssl
When running:
val caInput: InputStream = BufferedInputStream(FileInputStream("rest_of_the_world_production.crt"))
I get the error
Caused by: java.io.FileNotFoundException: rest_of_the_world_production.crt: open failed: ENOENT (No such file or directory)
and an instant crash when trying to access the file. The file is currently stored as a crt file under res/raw/rest_of_the_world_production.crt so why can't Android find it?
Do it as follows:
InputStream cert = context.getResources().openRawResource(R.raw.my_cert);
// Place your 'my_cert.crt' file in `res/raw`
So turns out I had imported
android.R
instead of
com.myApp.R
Changing this allowed me to find my certificate and import it.

How to post audio file to server in android

I need to post Audio file(picking from Audio/* intent) to server.here i got Uri, but the posting param type is File. I Used below code but it shows /directory/.. is not a absolute path and getting FileNotFoundException. Any one suggest me..
Code :
Uri uri=data.getData();
File f=new File(uri.getPath());
Log:
09-26 21:20:19.735: I/System.out(920): java.io.FileNotFoundException: /document/audio:5257: open failed: ENOENT (No such file or directory)
(this is a duplicate)
See here:
Convert file: Uri to File in Android
Don't use Uri::ToString() and use Uri::getPath() instead.

Cant read file name with Russian char from FileInputstream

I read some files from phone storage (which are in Russian name) i listed all files it will give me all file path correct but the time when i try to read from new FileInputStream(file) it gave me file not found exception i don't know why.
my code is like the following :
FileInputStream file1 = new FileInputStream(file); //this line gave error and file path is /storage/emulated/0/bhanuдосвидания.txt
and logcat is the following :
02-10 15:48:58.000: W/System.err(16013): java.io.FileNotFoundException: /storage/emulated/0/bhanuдосвидания.txt: open failed: ENOENT (No such file or directory) 02-10 15:48:58.020: W/System.err(16013): at libcore.io.IoBridge.open(IoBridge.java:409) 02-10 15:48:58.050: W/System.err(16013): at java.io.FileInputStream.<init>(FileInputStream.java:78) 02-10 15:48:58.050: W/System.err(16013): at com.S3Upload.UploadActivity$allCalculation.doInBackground(UploadActivity.java:16‌​71) 02-10 15:48:58.080: W/System.err(16013):
please help me...
May be the problem is like you change your file name to lower case. Try to remove lower case from your code and check.
please try to do something like the following :
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(file), "UTF8"));

java.io.FileNotFoundException: /sdcard/DCIM/ROBIN.jpg (No such file or directory)

I'm getting an error on the following line of code:
FileInputStream is = new FileInputStream("/sdcard/DCIM/ROBIN.jpg");
The error is:
java.io.FileNotFoundException: /sdcard/DCIM/ROBIN.jpg (No such file or directory)
But the image is present in the directory
My USB connection is Charge only
Never hardcode paths like /sdcard. For example, /sdcard is wrong on most Android devices. Use Environment.getExternalStorageDirectory() to find the root of external storage.
Make sure you have set the permission WRITE_EXTERNAL_STORAGE in your manifest.

Categories

Resources