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

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.

Related

Getting file access permission in a folder in Android 11/12

My app will read and write .db and .txt files in a directory in Downloads.
I already select and request that folder access permission like this:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_FOR_DIR);
It works fine if the directory is empty and create those files from app.
If I want to read or edit those files already exist will cause error:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/app_Folder/test.txt: open failed: EACCES (Permission denied)
I'm trying not to ask ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION.
How to let my app access those already exist files without using file selector?

getExternalFilesDir(null) gives java.io.IOException: Read-only file system

I'm trying to write a text file in the root folder of my app with the below code
File file = new File(getExternalFilesDir(null), "values.txt");
file.createNewFile();
All i am getting is java.io.IOException: Read-only file system
i have uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" in the manifest file.
How can i fix this issue ?
I have tried it on an emulator and a nexus 7 and cant seem to find what is causing the issue.
I see a lot of answers talking about remounting my file system but i can expect everyone that downloads the app to do that.
getExternalFilesDir(null)
Will return, The path of the directory holding application files on external storage. Returns null if external storage is not currently mounted so it could not ensure the path exists; you will need to call this method again when it is available.
So In your case we don't know whether external storage is mounted or not, but as you want to put file on root folder of your app. just use getFilesDir(). It will store your file in internal storage where your application files are stored.

How to read files placed in root-folder?

I want to know how to read files placed in root folder. For instance let's take file /init.trace.rc. When I'm trying to read it I get exception:
java.io.FileNotFoundException: /init.trace.rc: EACCES (Permission denied)
I thought that I need to get superuser access. I did it but that made no difference ((( I still can't read it. Could anybody help me? ...and also I wonder how file explorers like "RootExplorer" read that stuff?
apparently this is all you need to run to get root access:
Process root = Runtime.getRuntime().exec("su");
taken from:
How can I get root permissions through the Android SDK?

FileNotFoundException on Nexus 4

in my app I need to save some files in the device, to save the file I use:
Environment.getExternalStorageDirectory().getAbsolutePath() + "mypath"
to get the path. This work on almost all the devices but I have some problems in other devices (nexus4 for example..)
the error is:
java.lang.RuntimeException: java.io.FileNotFoundException:
/mnt/sdcard/musicfall/data/cache/.first_activation: open failed: ENOENT
(No such file or directory)
The strange thing is that it works on many device, and for this reason I can't see the problem.
PS: in "mypath" I use mkdir to create the folders that I need

W/DownloadManager( 7621): Aborting request for download 11:

E/UpdatesSettings( 7146): File write failed: java.io.IOException: open failed: EBUSY (Device or resource busy)
I/DownloadManager( 7621): Initiating request for download 11
W/DownloadManager( 7621): Aborting request for download 11: while opening destination file: java.io.FileNotFoundException: /storage/sdcard0/sysupdater/***.partial: open failed: EBUSY (Device or resource busy)
D/DownloadManager( 7621): cleanupDestination() deleting /storage/sdcard0/sysupdater/***.partial
I use DownloadManager to download file,sometimes it come out like this. Can anyone tell me why and how to solve this problem??
I've encountered a similar problem.
To avoid FileNotFoundException, make sure you:
Add the required permissions to write to external storage (if that's where you're trying to save), add the following into the AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Create the subfolder you're attempting to download into:
File folder = new File(FOLDER_PATH);
if (!folder.exists()) {
folder.mkdir();
}

Categories

Resources