Can't get path to internal storage - java

I'm using the Android-Download-Manager-Pro library from GitHub for downloading files on android.
After the download is finished, in the logs I see this:
Create file address: /storage/emulated/0/Downloads/uHlNhjA7c_g_27.jpg
It's a path to a file, which was downloaded a moment ago. When I try to create the File value and access it, I get a NullPointerException:
File file = new File("/storage/emulated/0/Downloads/uHlNhjA7c_g_27.jpg");
When I look at the length of the file in logs, it's always 0. How do I correctly get access to the downloaded file?

Try this:
Environment.getExternalStorageDirectory(). Don't be confuse with the name of the function. Read this in the documentation:
Note: don't be confused by the word "external" here. This directory
can better be thought as media/shared storage. It is a filesystem that
can hold a relatively large amount of data and that is shared across
all applications (does not enforce permissions). Traditionally this is
an SD card, but it may also be implemented as built-in storage in a
device that is distinct from the protected internal storage and can be
mounted as a filesystem on a computer.

i found the solution, i forgot to add permissions to manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

This adress could be the right one. Not sure for that:
File saveFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) , "uHlNhjA7c_g_27.jpg");
Acording to docs:
Standard directory in which to place files that have been downloaded by the user.

Related

Android 11 Storage Access and Java File Library

I have an android application already available on playstore. It is a file transfer app between PC to Android using localhost. I'm using following java function within it:
To create directory/file/show list of files and many other file functions(append, byte read/write etc):
file.mkdir
directory.listFiles();
file.createNewFile()
I had also put
requestLegacyExternalStorage="true"
And it is working fine on android 10 after giving sufficient read/write external storage permission.
But the problem is that these will not work on android 11 as requestLegacyExternalStorage will not work. I have used MANAGE_EXTERNAL_STORAGE and the app works perfect but the problem is that playstore is not allowing it. (Don't know why they rejected the update)
I have also used getExternalFilesDir and it works perfect but don't like data to be in android folder and remove if app is deleted.
I have also tried to use download location and it works but the problem is getExternalStorageDirectory is deprecated:
File file = new File(Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_DOWNLOADS,"FileChat");
So what I wanted to ask how can I manage/use file library for android 11 to read/write/create/delete files and directory. It should not be in android folder as this will be deleted after app is deleted. Can I used public directory like downloads? If I can, what is the alternative to getExternalStorageDirectory()?
I don't need to read user data. Just want to have a directory in which app can store files. The read function is working as file is picked by user using intent.
Thanks in advance.
Top-level directory write access of shared storage is not available in Android 11, however you can use the existing File APIs to access the media and download collection folders like /Pictures, /Downloads, /DICM etc. It won't be possible to create your app's own folder as top level directory but it is possible to create a dedicated sub-directory in the default collection folders.
To keep your app running as expected on Android 10, requestLegacyExternalStorage flag will also be required since access to shared storage on Android 10 was restricted via MediaStore APIs only.
If transfer is only for your apps own files then put them in
File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS),"FileChat");
Or use Storage Access Framework to let the user choose and or create a directory on external storage inclusive removable micro sd card.

Accessing the SD-Card in an Android Application

I am currently developing an Android Application, that will open and save .txt files.
In order to make it easy to read external .txt files i want to make sure, that they are saved in the app's folder on the SD-Card.
Unfortunately i am only able to access /storage/emulated/0.
I already tried 2 different possibilites:
Environment.getExternalStorageDirectory()
getExternalFilesDir("MyFileStorage")
They both lead to /storage/emulated/0 on the internal storage.
Does anyone know how i can access the app's folder on the SD Card of my Smartphone (USB-Debugging).
that they are saved in the app's folder on the SD-Card
By default, apps do not have a directory on removable storage.
On Android 4.4+, you are welcome to use getExternalFilesDirs() (note the s on the end). If this returns a list with 2+ entries, the first one will be on external storage, and the others will be on removable storage.
I already tried 2 different possibilites... They both lead to /storage/emulated/0 on the internal storage
Those point to what the Android SDK refers to as external storage.
Apps don't come with a folder on the external storage by default. You will need to create a folder for your app like this:
String folder_main = "YourAppName";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
Remember that you need to add the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to your AndroidManifest.xml to access the SD card (and use the permissions model introduced in Android 6.0 for newer devices).

Opening cached file in android

I'd like to open a file that I written copied to my cacheDir (e.g audio file, or image) with an intent, but it seems like other apps don't have the permission to this directory. It's quite reasonable (my logic tells me that only root and the app have the access to this folder), but is there any workaround for this issue?
Well, that's quite obvious my friend, use activity.getExternalCacheDir() to obtain a cache dir on your SD card. Just don't forget to set the permissions.

Custom password encrypted file system for storing files in Android

is it possible to create a custom file system or use an existing file system like EncFs to read data inside Android environment.
My idea is to make a virtual filesystem for storing some media files and developing an app which can directly access those files from the filesystem volume.
I hope im clear with my question.!!
Yes, but you need a rooted devices to use it. Or, you need to build your own firmware. Here's one, it's open source: http://nemesis2.qx.net/pages/LUKSManager

Difference between getExternalFilesDir and getExternalStorageDirectory()

I understand that ExternalFiles is to be used on API 8 and up and getExternalStorageDirectory is for 7 and down. However I am a little confused between the use. For example I wanted to check that a folder that exists and previously you would use something like:
File ChildFolder = new File(Environment.getExternalStorageDirectory() + "/ParentFolder/Child");
However every example I see says to use getExternalFilesDir (null), File.ext. Since I am above API 8 I want to use this method but how do I just check for a folder? I will check for a files existence at another point but for now just want to see if the folders exist??
getExternalFilesDir()
It returns the path to files folder inside Android/data/data/your_package/ on your SD card. It is used to store any required files for your app (e.g. images downloaded from web or cache files). Once the app is uninstalled, any data stored in this folder is gone too.
getExternalStorageDirectory()
It returns the root path to your SD card (e.g mnt/sdcard/). If you save data on this path and uninstall the app, that data won't be lost.
First of all, we need to understand what is difference between Internal Storage, External Storage (aka primary external storage), and Secondary External Storage?
Internal Storage: is storage that is not accessible by the user, except via installed apps (or by rooting their device). Example: data/data/app_packageName
Primary External Storage: In built shared storage which is "accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer". Example: When we say Nexus 5 32 GB.
Secondary External Storage: Removable storage. Example: SD Card.
getExternalFilesDir (String type)
It returns the path to files folder inside Android/data/data/your_package/ on primary external storage. Which is inbuilt storage.
Environment.getExternalStorageDirectory()
It will return the path of the secondary external storage directory
! IMPORTANT UPDATE ! for whoever comes across this question.
As this is a somewhat old question just wanted to provide some additional information. Since KitKat even apps that have WRITE_EXTERNAL_STORAGE permission are only allowed to write to Android/data/data/your_package/ on external storage, a.k.a getExternalFilesDir()
If you will try to write to getExternalStorageDirectory() + "/somefolder/anotherfolder/" you will get a SecurityException on most devices
!! IMPORTANT !!
Environment.getExternalStorageDirectory() is deprecated and Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT, should be used instead.
This method was deprecated in API level 29.
To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.
https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
Also beginning from Android.M developers need to ask for permissions at run time.
See more details in documentation here and this question
Environment.getExternalStorageDirectory() deprecated in API level 29 java

Categories