Get path for data storage - android

I need to create file in storage card from my Android program. I know I can create this file only in particular location like \Card\Android\data\my.application.package\. How to get this directory exact location? Code below brings path /data/my.application.package/Log and mkdirs() does not creates directories.
String path = "Log";
File root = new File(Environment.getDataDirectory().getAbsolutePath(),
"my.application.package" + File.separator + path);
boolean dirExists = true;
if (!root.exists()) {
dirExists = root.mkdirs();
}
How to get the same application path on internal storage?

You should use getFilesDir() for getting app specific internal File path and getExternalFilesDir() for getting app specific external File path.
Note that on Android 4.3 (API level 18) or lower, your app need to request storage-related permissions to access app-specific directories within external storage.
For more information see https://developer.android.com/training/data-storage/app-specific

Related

Unable to save data in SD Card Directory

I have write a code to find if SD card path if available which is like this
File[] paths = ContextCompat.getExternalFilesDirs(context, null);
if (paths.length > 1) {
if (paths[1] != null) {
root = paths[1].getAbsolutePath();
// for sd card
} else {
root = Environment.getExternalStorageDirectory().toString();
}
} else {
root = paths[0].getAbsolutePath();
}
I am saving my data in path "/storage/4130-1912/Android/data/com.enable/files" but I wanted to save data outside Android folder.
I have also tried to make an directory outside the Android folder.But unable to make it.I am testing in Lave phone with version Marhmallow
As you are using Marshmallow, you should be requesting runtime permissions (more details here). Despite that, you may add the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE for external storage access. You can find more information on how to here.
You can access the external storage path like this: Environment.getExternalStorageDirectory().getAbsoluteFile(). If you are getting FileNotFoundException, it's probably because you didn't add an additional "/" before your file. Example:
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +
"/your_file.txt");
I am saving my data in path "/storage/4130-1912/Android/data/com.enable/files"
Ok. Go ahead. You will succeed.
but I wanted to save data outside Android folder.
Well that is not possible anymore on modern Android systems.
Even inside the Android folder you can only write to mentioned private directory for your app.
If you want to write to the whole micro SD card then use the storage access framework.
This will help you
As miguelarc said:
As you are using Marshmallow, you should be requesting runtime
permissions (more details here). Despite that, you may add the
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE for external
storage access.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + "/yourdir");
if (f.mkdirs() || f.isDirectory())
Log.e("path",f.getPath());
//do what you want
}

Create a file folder for app

The below code doesn't create a folder in my device.
String intStorageDirectory = context.getFilesDir().toString();
File folder = new File(intStorageDirectory, "test");
folder.createNewFile();;
I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?
With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.
To save files externally(This doesn't mean saving to SD Card) you can create directory and file with
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs()
}
File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);
And you need some kind of OutputStream to write data to that file.
Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above
new File(context.getFielsDir(), "test").mkdirs();
createNewFile creates a file, not a folder. Using mkdirs instead of mkdir ensures that all parents exist. There's also no reason to go through a string when you already have a File.
Adding folder.mkdirs(); should work in place of folder.createNewFile(); And don't forget to add the permissions.
This will create a folder in you data directory.
And just a suggestion , if you want to store media in a SD card folder maybe Environment.getExternalStorageDirectory() is good.

How could I create a file in the "Internal Storage" Directory?

I have a Samsung Galaxy S6. I'm currently working on a test application where I would like quick access to a folder with my files.
Using the provided "My Files" Application, it specifies that all those folders are in the "Internal Storage" folder.
I know that internal storage is private, but I want to create a folder in the default folder that windows accesses when the phone is plugged in.
For example, the following code does not create the directory in the correct location.
File storage = new File("/testappplication");
if(!storage.exists()) {
storage.mkdir();
System.out.println("Folder Created");
}
I just want to know the path where to create the folder. Many other applications have storage here, so I know its possible.
You can't create a directory inside the internal storage of the device. Except you've a root access for the app.
So, the following code won't work:
File storage = new File("/testappplication");
Because it tell the app to create a testappplication in the root folder.
You can only create the directory inside your app private folder within the following path:
String path = getFilesDir().getAbsolutePath();
And make the folder using the path.
Or you can use something like this:
File folder = new File(context.getFilesDir(), "testappplication");
if (!folder.exists()) {
folder.mkdirs();
} else {
// folder is exist.
}
Read more at Saving Files
First just for trial make runtime permmision and then try the following code
private void createInternalFile() {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()+"/"+getApplicationContext()
.getPackageName()+"/File/profile");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
} }
then check your internal storage in which you will find a folder whose name is your package name
After a while later I found the answer to this question.
public void createDirectory() {
File file = new File(Environment.getExternalStorageDirectory(), "/test");
if (!file.exists()) {
file.mkdirs();
Log.w("DEBUG", "Created default directory.");
}
}
This is how you create it code wise. The reason it wasn't creating was due to Samsungs weird permissions.
Make sure you have the storage permission enabled in Settings -> Apps -> App Name -> Permissions. I needed to turn it on so it would create the folder.

getting data from internal storage

I'd like to read all files located in internal storage and filter them to find those *.mp3 ones. With external storage I could just use:
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath();
to get the path, but I cannot find the way to do it for internal memory (for instance to get to Music folder of the phone). Is there a way to do it?
Are you aware of getFilesDir() method?
http://developer.android.com/training/basics/data-storage/files.html
You can use Context.getFilesDir() method to get the path to the internal storage.
Like : File file = new File(getFilesDir() + "/" + name);

Android, issue with finding saved file on storage

I am downloading a file(PDF) from server using DownloadManager and request is
request.setDestinationInExternalFilesDir(this,Environment.getDataDirectory().getPath(), filename);
The File gets save in my internal storage under
"MyFiles->All files/DeviceStorage/Android/data/myPackage/files/data/"
For opening the saved file i use the following code
File sdCardRoot = new File(Environment.getDataDirectory().getPath());
if(sdCardRoot.exists()){
for (File f : sdCardRoot.listFiles()) {
if (f.isFile())
name1 = f.getName();
String path = f.getPath();
Log.e("UriFile", ""+Uri.parse(path));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(path), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
But it shows document cannot be opened, But i can see the file in the location and can open it directly not from my app.
I have a SD card inserted and there is a similar folder structure created in sdcard as well .
I guess while opening the file it searches the folder on SDCARD not my internal storage, how to solve this.
Have you added permissions in AndroidManifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
As your file is stored in app internal storage i.e /data/data/packagename/ use getFilesDir() to get the path of directory holding application files & use listFiles() to get all files inside returned path directory.
getFilesDir() - Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored. No permissions are required to read or write to the returned path, since this path is internal storage.
OR you can use getExternalFilesDir(String type) to get the path of directory where you app files are saved & iterate through files using listFiles().
getExternalFilesDir(String type) - Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Environment.getExternalStorageDirectory()) where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.
Usage Example:
public void listFilesInAppInternalStorage(){
//using getFilesDir()
File filesDir = getFilesDir();
File filesList[] = filesDir.listFiles();
for(File file: filesList){
Log.i(TAG, "FileName:" + file.getName());
}
//using getExternalFilesDir()
File extFilesDir = getExternalFilesDir(null);
File listOfFiles[] = extFilesDir.listFiles();
for (int i=0; i < listOfFiles.length; i++)
{
Log.v(TAG, "FileName:" + listOfFiles[i].getName());
Log.d(TAG, "FilePath:" + listOfFiles[i].getAbsolutePath());
}
}

Categories

Resources