I would like to store some cache data in the path /sdcard/Android/data/, but how can I get it?
simply use Context.getExternalCacheDir(), this is the standard way android provides for external cache, when your app uninstalled, the dir removed automatically and don' t leave any trash for the user.
the dir pattern ends like Android/data/your-package-name/cache/
but you should check null for the return value (a File object), if null, it indicates that the cache dir is not avaialbe(like sd card removed or connected to your pc, etc.)
I am doing this before saving my files to my external folder.
String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(pathToExternalStorage + "/" + getText(R.string.app_name));
if (!appDirectory.isDirectory() || !appDirectory.exists()) //Checks if the directory exists
appDirectory.mkdir();
Related
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
}
I am working on a chat application where I am sending/receiving Images/ Media. I am writing those files in a directory both sent and received.
Now the problem is if I send an image from gallery in chat I am copying it into Sdcardpath + AppName/Images/Sent/.
I this case the images in sent folder are duplicate.
And gallery app is Showing Sent folder with images. I need a way so that gallery cannot read the SentFolder. Below is my code for creating directory.
public static File getImageSentDirectory() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File dir = new File(Environment.getExternalStorageDirectory() + DOCUMENT_FILE_SENT_PATH);
if (!dir.exists())
dir.mkdirs();
return dir;
} else {
File dir = new File(getAppContext().getFilesDir() + DOCUMENT_FILE_SENT_PATH);
if (!dir.exists())
dir.mkdirs();
return dir;
}
}
I also tried .
dir.setWritable(true,true);
dir.setReadable(true,true);
But on restart device gallery app is showing the sent folder with images .
Add a .nomedia file in your "Sent" directory.
This will make the "Gallery" app skip your folder and thus it won't be listed.
The .nomedia file is like this inside WhatsApp media directory. It is a zero byte file.
I need a way so that galery can not read the SentFolder.
Do not put SentFolder on external storage. Put it on internal storage.
The .nomedia suggestion may help, but it is a convention, not a rule. Any app that has read access to external storage can get to any files you put on external storage. If you do not want that, do not put the files on external 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);
I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE
Which is correct,
String filePath = Environment.getExternalStorageDirectory()
+ "/data/com.packagename";
or
String filePath = Environment.getExternalStorageDirectory()
+ "/Android/data/com.packagename";
if I want to store data in external storage? I see many apps are using the second option, but some use the first path.
You should rely on the API to figure out the directory for you:
File externalDir = Context.getExternalFilesDir(null);
Context.getExternalFilesDir will return your 2nd path. Programs that return the 1st path probably hardcoded the path and got it wrong as a result.