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 {
}
Related
Thanks for others help , i can create folder succeed now.
But i get a strange issue if android emulator doesn't has sdcard, i can't create the folder.
I can't figure it out..
Here is my code:
try {
if (Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)) {
System.out.println("can be read and write");
File sdFile = android.os.Environment.getExternalStorageDirectory();
//String path = sdFile.getPath() + File.separator + "DestPdf";
String path = sdFile.getPath() + "/DestPdf";
File dirFile = new File(path);
if (!dirFile.exists()) {// if folder doesn't exist
System.out.println("create file");
dirFile.mkdirs();// create file
System.out.println(dirFile.toString());
}
}
} catch (Exception ex) {
ex.toString();
}
If my android emulator has sdcard root, i can create the folder DestPdf:
If there is no sdcard , i print the root is /storage/emulated/0/DestPdf, i create the folder failed, the folder emulated has nothing...
Any help would be appreciated . Thanks in advance.
you can use your app Internal Package folder for saving data,
context.getFilesDir().getPath()
context is here Activity instance.Ok?
Do with it..
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 have been successfully able to create files in my apps local directory using the below code
File appDir = new File(this.getExternalFilesDir(null), "my_folder");
if (!appDir.exists())
{
boolean result = appDir.mkdir();
if (!result) {
Log.i(Util.TAG, LOG_LABEL
+ ":: Unable to create \"my_folder\" Directory : "
+ appDir.getAbsolutePath()
+ " Directory already exists !");
}
}
File HTMLFile = new File(appDir,"html.txt");
But now when I tried to do the same from a service, the file was not being created, I even checked using 'HTMLFile.exists()' and it says the file does not exist.
My question is, Is it actually possible to create files from a service? or am I missing something here.
It is possible to write to the storage of your android: by the help of the following methods
Make sure you provide uses permission to write and read from storage in manifest file.
I have used this code:
private File newFile;
public ListenNotification() {
Log.d("Service","InConstrutor");
newFile=newFile(Environment.getExternalStorageDirectory(),"NotificationFromService");
if(!newFile.exists()){
Log.d("Service","Created");
newFile.mkdir();
}
else{
Log.d("Service","Existed");
}
}
File HTMLFile = new File(newFile,"html.txt");
I develop an app which collects some data from internet. Then save it to a temporary folder. To build this app I need to create and access a folder ( just for the purpose of app, not for the user). How can I do it?
this code is to create folder:
File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");
if(!direct.exists())
{
(direct.mkdir()) //directory is created;
}
try it may help you
File mFile;
onCreate()
mFile= new File(Environment.getExternalStorageDirectory()+"/temp/";
mFile.mkdir();
onDestroy();
mFile.delete();
try out this...
private void makeFolder(){
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + getString(R.string.folder_name));
boolean mainfolderexist = root.exists();
if (!mainfolderexist) {
try {
if (Environment.getExternalStorageDirectory().canWrite()) {
root.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
All The best
You should really check this other SO answer: https://stackoverflow.com/a/6485850/65716
Aside from the fact that you have to completely manage your use of the space, etc, caching on external storage requires more permission for your app.
See http://developer.android.com/reference/android/content/Context.html#getCacheDir()
"Apps require no extra permissions to read or write to the returned path, since this path lives in their private storage."
For app use only, I would recommend to use Context.getDir() for retrieving the directory if the files is used by our app only and don`t want to be visible to users by file browsers.
// No need to check if exist, created automatically.
File tempRoot = context.getDir("temp", Context.MODE_PRIVATE);
// do something
Am using sdcard for saving data ,my problem is that when app is running and some one delete this folder then the application fail. Is there anyway to notify the app , when this folder get deleted ?. Please help me
Try like this.
private static File logFile = null;
String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
logFile = new File(SD_CARD_PATH + "/" + "folderName");
if (logFile.exists()){
//exist.so do your work
}
else{
//not exists
}