If you are developing an app that needs to access files from the internal storage of your Android device, you may encounter the java.io.FileNotFoundException: /storage/emulated/0/download/ error. This error means that your app cannot find or open the file that you are trying to read or write. In this article, we will explain what causes this error, how it affects your app, and how you can fix it using four different methods.
The java.io.FileNotFoundException: /storage/emulated/0/download/ error is a type of IOException that occurs when your app tries to access a file that does not exist or is not accessible. The /storage/emulated/0/download/ path is the default location for downloaded files on Android devices. However, this path may not be available or visible to your app due to various reasons.
Some of the possible causes of the java.io.FileNotFoundException: /storage/emulated/0/download/ error are:
Some of the symptoms of the java.io.FileNotFoundException: /storage/emulated/0/download/ error are:
Depending on the cause and the target version of your app, you can use one of the following methods to solve the java.io.FileNotFoundException: /storage/emulated/0/download/ error:
If your app does not have the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions, it cannot access files from the internal storage. To grant these permissions, you need to declare them in your manifest file and request them at runtime from the user. Here are the steps to grant storage permissions to your app:
private boolean checkStoragePermissions() int readPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); int writePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); return readPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED; private void requestStoragePermissions() ActivityCompat.requestPermissions(this, new String[]Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_REQUEST_CODE); @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == STORAGE_REQUEST_CODE) if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) // Permission granted, proceed with file access else // Permission denied, show a toast or a dialog If your app is targeting Android 10 or higher and does not support scoped storage, you can use the android:requestLegacyExternalStorage="true" attribute in your manifest file to opt out of scoped storage and access files from the internal storage as before. However, this is a temporary solution and will not work on Android 11 or higher. Here are the steps to use android:requestLegacyExternalStorage="true" attribute:
android:requestLegacyExternalStorage="true"If your app only needs to read or write a single file from the internal storage, you can use the ACTION_OPEN_DOCUMENT intent to let the user pick the file from a system file picker. This way, you do not need to request any permissions or opt out of scoped storage. Here are the steps to use ACTION_OPEN_DOCUMENT intent:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.setType("application/pdf"); // For example, if you want to access PDF filesstartActivityForResult(intent, FILE_REQUEST_CODE);@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILE_REQUEST_CODE && resultCode == RESULT_OK && data != null) Uri uri = data.getData(); // This is the URI of the selected file // Use a ContentResolver to open an InputStream or an OutputStream to read or write the file If your app is targeting Android 11 or higher and needs to access multiple files from the internal storage for a legitimate purpose, you can request the MANAGE_EXTERNAL_STORAGE permission in your manifest file and let the user confirm it in the settings. This permission allows your app to access all files from the internal storage without opting out of scoped storage. However, this permission is considered dangerous and should be used sparingly. Here are the steps to request MANAGE_EXTERNAL_STORAGE permission:
private boolean checkManageExternalStoragePermission() return Environment.isExternalStorageManager(); private void requestManageExternalStoragePermission() Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivity(intent); In this article, we have explained what is the java.io.FileNotFoundException: /storage/emulated/0/download/ error, what causes it, and how to fix it using four different methods. We hope that this article has helped you to solve this error and access files from the internal storage of your Android device. If you have any questions or feedback, please let us know in the comments below.
Here are some frequently asked questions about the java.io.FileNotFoundException: /storage/emulated/0/download/ error:
A: Scoped storage is a feature introduced in Android 10 that limits the access of apps to files from the internal and external storage. Scoped storage aims to improve the privacy and security of users' data by giving them more control over which apps can access their files. Scoped storage affects file access because it restricts the use of file paths and requires apps to use other methods such as intents, content providers, or media store APIs to access files.
A: You can check if your app supports scoped storage by looking at the targetSdkVersion attribute in your manifest file. If your targetSdkVersion is 29 or higher, your app supports scoped storage by default. If your targetSdkVersion is lower than 29, your app does not support scoped storage unless you opt in by using the android:preserveLegacyExternalStorage="true" attribute in your manifest file.
A: You can test your app for scoped storage compatibility by using the Android Emulator or a physical device running Android 10 or higher. You can also use the adb shell command to change the value of the external_storage_legacy flag to enable or disable scoped storage for your app.
A: Some of the best practices for accessing files from the internal storage are:
A: You can find more information about file access on Android by visiting these links: