I would like to create a directory in /storage/emulated/0/ and a save a file there.Since the "getExternalStorageDir()" & "getExternalPublicStorageDir()" are Depreciated I don't know how to implement it.
I have gone through most of the questions and answers but they are all either outdated/open.
All I want is a way to access the "storage/emulated/0/" path.
val extStorageDirectory = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
val folder = File(extStorageDirectory, "Work Logs")
folder.mkdir()
The above code creates a folder in "Android/data/com.xxx.xxx/"
Easy with MediaStore.
binding.createFolderButton.setOnClickListener {
val values = ContentValues()
values.put(MediaStore.MediaColumns.RELATIVE_PATH, "${Environment.DIRECTORY_DOCUMENTS}/myFolder/") //folder name
contentResolver.insert(MediaStore.Files.getContentUri("external"), values)
Toast.makeText(this, "\"myFolder\" created", Toast.LENGTH_SHORT).show()
}
Demo: https://youtu.be/a6Q7IlA_uOs
Related
Folder is creating Successfully but the location is something like storage/emualted/0
I'm using Enviroment.getExternalStorageDirectory methods
I want to create publicily available folder which i could save my app's data what should i do?
use Context#getFilesDir() orContext#getCacheDir()
it was restricted from google. below android 7 only u can create a folder inside internal folder. after that u only can create folder inside downloads,music and alarm
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(folder, "example.txt");
if (!file .exists()){
file .mkdirs();
}else{
}
Finally it worked in Android 10
//by Using This Method
private void createFolder(String name) {
File mydir = getApplicationContext().getExternalFilesDir(name); //Creating an internal directory;
if (!mydir.exists()) {
mydir.mkdirs();
Toast.makeText(this, "Created" + " -> " + mydir.getAbsolutePath(), Toast.LENGTH_SHORT).show();
} else {
}
How to access /storage/emulated/0/Android/data/com.abc.appName folderpath?
When I am trying to access in that way:
String path = Environment.getExternalStorageDirectory()+"/Android/data";
File directory = new File(path);
File[] files = directory.listFiles();
It is not returning anything, which means the files is null.
Simply, you can use
context.getExternalFilesDir(String type)
where type parameter refers to the type of file e.g. Environment.DIRECTORY_PICTURES or Environment.DIRECTORY_MUSIC, etc...
if you would like to access the folder itself, just pass null to getExternalFilesDir method.
val folder = applicationContext.getExternalFilesDir(null)
val files = folder?.listFiles()
Log.d("Your Tag", "onCreate: ${folder?.path}")
Log.d("Your Tag", "onCreate: Your files are ${files}")
this will return the path as:
/storage/emulated/0/Android/data/*yourPackageName*/files
Your files are ....
I'm trying to download a file and if it exists to delete it and download again. The download works. But the function checkFile never finds the file and I don't know why.
private fun startDownloading() {
checkFile()
val url = etUrl.text.toString()
val request = DownloadManager.Request(Uri.parse(url))
//allow type of networks to download file(s) by default, by default both are allowed
request.setAllowedNetworkTypes((DownloadManager.Request.NETWORK_MOBILE or DownloadManager.Request.NETWORK_WIFI))
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "$folderName/$fileName")
//get download service, and enqueue file
val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
manager.enqueue(request)
}
private fun checkFile(){
val file = File(Environment.DIRECTORY_DOWNLOADS, "$folderName/$fileName")
if(file.exists()){
Toast.makeText(this, "File exists", Toast.LENGTH_LONG).show()
file.delete()
}else{
Toast.makeText(this, file.toString() + " doesn't exist", Toast.LENGTH_LONG).show()
}
}
Thanks in advance.
You create the file in the public external storage directory with:
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"$folderName/$fileName"
)
but you try to read it, from a relative (from the application's current directory) path:
val file = File(Environment.DIRECTORY_DOWNLOADS, "$folderName/$fileName")
You need to use the downloads directory, relative to the public external storage directory:
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "$folderName/$fileName")
So I've built some code to download a file which works fine and I have set it to download into the applications directory which works. it's stored in the application folder /files/dltest
My issue is with checking programatically wether or not the file exists, I've tried methods one stackoverflow and for some reason I can only get my hard coded path to work.
/sdcard/Android/Data/com.test.alihassan.download/files/dltest/REQS.pdf
Using built in methods to retrieve the path gives me the same path but with /data/data/com.... and this doesn't work
File mydir = context.getFilesDir();
File fileWithinMyDir = new File(mydir, "myfile/path/fileNmae");
if(fileWithinMyDir.exists()){
//exists
}else{
//not exists
}
Update:
//File mydir = this.getFilesDir();
File mydir = this.getExternalFilesDir("/dltest/REQS.pdf");
if (mydir.exists()) {
//exists
} else {
//not exists
}
I want to create a directory on sd card keeping it as a separate activity in one of my application. I wrote the following code in the onCreate() of the application. It is not creating the directory though this code works fine if I try to implement it as an independent application.
Please suggest a solution for this problem.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
String dirName = "/sdcard/TEST";
File newFile = new File(dirName);
newFile.mkdirs();
Log.d("CaptureTest.java","Directory created");
if(newFile.exists()){
Log.d("capturetest.java","directory exists");
if(newFile.isDirectory()){
Log.d("capturetest.java","isDirectory = true");
}
else Log.d("capturetest.java","isDirectory = false");
} else
{
Log.d("capturetest.java","directory doesn't exist");
}
} catch(Exception e){
Log.d("capturetest.java","Exception creating folder " + e);
}
........................................
..........................................
}
The SD card might be mounted at /mnt/sdcard instead of /sdcard.
But the safest technique to get the external storage directory is like in the following code
File myDirectory = new File(Environment.getExternalStorageDirectory(), "my directory");
if(!myDirectory.exists()) {
myDirectory.mkdirs();
}
There could be a number of things causing this:
Check that external storage is available and writeable before trying to write to it.
Don't use String dirName = "/sdcard/TEST"; use Environment.getExternalStorageDirectory() or Context.getExternalFilesDir() instead.
This page has some really useful tips for correctly accessing the SD card.