How to open a directory saved on SD Card Android - android

I am programming a notepad android app but I am having difficulties with my Open button. How do i implement it so that when I click on it, a dialog box comes up with the .txt files saved in the folder I created on the SD card? Also, how do i load the chosen file to my current activity?
Thank You

Try the Following,
File f = new File(Environment.getExternalStorageDirectory() +"/"+ dirname);
if(f.isDirectory())
{
ArrayList<String> files= new ArrayList<String>();
File file = new File(Environment.getExternalStorageDirectory() +"/"+ dirname+"/");
File fileList[] = file.listFiles();
for(int i=0;i<fileList.length;i++)
{
files.add(filelist[i].getAbsolutePath());
//here you can get all files.
}
}

Related

How to find directories with particular name

Any app can create folder in external storage.
This folder can be placed at SD card or phone inner memory.
If i use this code, i can to get list of all my external storages.
File file2[] = getExternalFilesDirs(null);
it return:
/storage/emulated/0/Android/data/com.snailp4el.android.tatoeba/files
/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files
but if i try to get list of it path:
File f = new File("/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files");
File[] files = f.listFiles();
I get nothing.
in this case:
File f = new File("/storage/");
File[] files = f.listFiles();
i get:
/storage/8639-0FFD
/storage/emulated
/storage/self
That is OK. i finally get all storages in my phone.
Question is:
How to scan subfolders to find particular folders by name?
And is my approach right. I mean to scan "/storage"?
And if Yes will it work at all version?
and finally what is "/storage/self"?
I solved it this way.
May by it is not the best solution but it works fine for me.
public ArrayList getExternalFolderPath(String folderName){
ArrayList<String> arrayList = new ArrayList<>();
//get all storages in phone
File externalStorages[] = getExternalFilesDirs(null);
for (File es: externalStorages){
String exF = es.toString();
//cut out what don't need
exF = exF.split("/Android/data")[0];
File file = new File(exF);
File[] files = file.listFiles();
for (File f: files){
Log.i(TAG, "getAnkiExternalFolder()" + f.toString());
//add in array the path
if(f.toString().contains(folderName)){
arrayList.add(f.toString());
}
}
}
return arrayList;
}
You can get two directories(from both storage). The directory you need the last changed one.
file.lastModified()

Create nested folders into internal storage

I am currently working with internal storage. i want to create multiple folders like mainFolder/subFolder/fileName.
i already prepare lot of tutorials and i wouldn't find the best solution.. And i am also getting error java.io.FileNotFoundException: open failed: EISDIR (Is a directory).
My query was
What is this error? any one can please explain this.
How to create folders in internal storage.
Finally I got this One
To create Nested Folders
// Save Internal Storage
File myMainDir = context.getDir("MainFolder", Context.MODE_PRIVATE);
File mySubjectDir = new File(myMainDir, "subFolder");
mySubjectDir.mkdir();
File myModuleDir = new File(mySubjectDir, "semiSubFolder");
myModuleDir.mkdir();
File myFinalDir = new File(mySubjectDir, "fileName.mp4");
// Save External Storage
String DNAME = "MainFolder"+"/"+subFolder+"/"+semiSubFolder;
File rootPath = new File(Environment.getExternalStorageDirectory().toString(), DNAME);
if(!rootPath.exists()) {
rootPath.mkdirs();
}
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.v("Cannot use storage","Cannot use storage");
}
File myFinalDir = new File(rootPath,"fileName.mp4");
Do it like this :
String folder_main = "NewFolder";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
If you wanna create another folder into that :
File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_main, "product1");
if (!f1.exists()) {
f1.mkdirs();
}

How can we create a folder of app in device storage in Android?

I'm using code in comment to save a file in directory folder. This code is working properly with mobile phone having sd card.
But if I use it with mobile phone not having sd card it is giving IO exception.
How can I create a folder like whatsapp has in device storage root to save images etc but I want a folder of application name to save .csv files in my device storage root.
First make sure you have added Storage Permissions inside Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you want to create folder inside ExternalDirectory
File f = new File(Environment.getExternalStorageDirectory()+"/foldername");
if(!f.exists()) f.mkdir();
If you want to create file inside internal storage
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory;
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
You can create directory in internal storage and external storage as Follows.
//check whether Sdcard present or not
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
String directory = "/your Directory name or app name ";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + directory;
File dir = new File(path);
if(!dir.exists()) //check if not created then create the firectory
dir.mkdirs();
//add your files in directory as follows
File file = new File(dir, filename);
}
else
{
// create folder in internal memory
File mydir = context.getDir(directory, Context.MODE_PRIVATE); //Creating an internal directry
if(!mydir.exists()) //check if not created then create the firectory
mydir.mkdirs();
//add your files in directory as follows
File file = new File(mydir, filename);
I hope it will work for you

How to Store a File in our sd card that we have inserted

I am trying to save a file in my External SDcard but the file is getting stored in internal storage. How to store the file in the SDcard inserted
mr = new MediaRecorder();
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
Log.e("aa","sss");
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(), "Recorder");
} else {
cameraFolder= getDir("Recorder",MODE_PRIVATE);
}
if(!cameraFolder.exists()) {
cameraFolder.mkdir();
}
fname = cameraFolder + "/myrec1.MPEG_4";
There is already a similar question here, you can also find more detailed information on the android docs
As suggested in a comment below I'll improve this answer.
In order to save a file to a specific position in your SD card you need to create a File object which points to that position:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/your/file/directory");
dir.mkdirs();
File file = new File(dir, "filename");
and then use a FileOutputStream to write the content.
Remember that you need to add the required permission in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

where to write a text file and how to set it up for an sd card

I know this is a popular question but I have looked at all of the other responses and none of them seem to work. What I want to do is write some code to a text file. My first question: is there a way to view that text file without writing code to the console? Second, I dont know where in my phone it goes and I want to see it to help trouble shoot, so if you know how to do that too, it would be great. So now I will give you an overview of what is happening. When I start my program it checks to see if the file exists, if it doesn't it reads a file out of my assets folder and copies that info and sends it to a file into the sd card. If it exits it reads the info from the sd card. Next if I press a button and change numbers and print it to my sd card again then I close it using task managers then when I come back the original information is here. I don't feel like it is being able to find my sd card location. So far I have used.
File outfilepath = Environment.getExternalStorageDirectory();
String FileName = "ExSettings.txt" ;
File outfile = new File(outfilepath.getAbsolutePath()+"/TimeLeft/"+FileName);
File outfilepath = Environment.getExternalStorageDirectory();
String FileName = "ExSettings.txt" ;
File outfile = new File(outfilepath, FileName);
Any Ideas?
This is a function that I wrote which will take in a list array and write to a file line by line.
It will use the path in fileName to make a new folder called myfolder in the root of the SDCard, an inside will be your file of newtextfile.txt.
List<String> File_Contents = new ArrayList<String>();
File_Contents.add("This is line one");
File_Contents.add("This is line two");
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfolder/newtextfile.txt");
f.mkDirs();
try {
BufferedWriter out = new BufferedWriter(new FileWriter(f));
for (int x = 0; x < File_Contents.size(); x++) {
out.write(File_Contents.get(x));
out.write(System.getProperty("line.separator"));
}
out.close();
return true;
} catch (Exception e) {
return false;
}

Categories

Resources