I have searched everywhere on stackoverflow, there are plenty of similar questions, but the answer always ends up with something that helps write on the internal storage.
I am using the File Browser snippet available here: https://github.com/vaal12/AndroidFileBrowser
After that, I get the path to the directory where I want to create my Folder.
try {
print(Environment.getExternalStorageDirectory().getAbsolutePath()); //return something like "/storage/sdcard0/"
String realPath=newDir +"/"+ getString(R.string.folder_name); //returns something like "/storage/extSdCard/..."
//realPath=realPath.replaceAll("storage", "mnt");
print(realPath);
File f = new File(realPath);
if (!f.exists()) {
f.mkdirs();
}
} catch(Exception e) {
print(e.toString());
}
This works fine when the chosen directory is on the internal sd card, but nothing happens (not even an IOException) when it's on the removable external sd card
There is good description and example about saving onto external storage on the android developer webpage. I think this would help you: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
Related
Currently, we have an app that we are targeting Android 10 and right now are using the legacy storage API. Our app communicates via Bluetooth sensors and reads and writes raw data in CSV files in a subfolder in the main directory, with that subfolder having subfolders for each user.
I know Android 11 will enforce Scoped Storage. I would like to know, is our use case outside of the Scoped Storage requirement? It appears our use case isn't supported by MediaStore. If not, how would we go about this?
MediaStore APIs are just for media files - images, videos, and audio.
You can store all files in the app's private folder and add an export option to your app (maybe compress the whole structure to an archive). So a user will be able to store or send it wherever they want.
In this case, you need to use FileProvider to expose the file from the private directory.
reads and writes raw data in CSV files in a subfolder in the main directory,
For an Android 11 device you can create your own folders an subfolders in the Documents directory of what you call the 'main folder'.
And for using the MediaStore: you can also write any file to that Documents directory. Well in a subfolder if not directly.
I'm in a similar boat. This may help you get started.
public class FirstFragment extends Fragment {
...
public void fauxMakeCsvSurveyFile() {
File appDir = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "Field_data");
appDir.mkdirs();
try {
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/Field_data/" + "OutputFile.csv");
FileOutputStream fos = new FileOutputStream(file);
String text = "Hello, world!";
fos.write(text.getBytes());
fos.close();
}
} catch (IOException e) {
Log.e("IOException", "exception in createNewFile() method");
}
}
...
}
I know there have been questions about this, but for some reason nothing seems to work for me.
I'm trying to get 2 text files to save to the SD card from my app. It correctly creates the directory and the files, but always to the Internal Storage, never the External Storage. I do have the permissions in place as well in the Manifest.
try {
File sdCard = Environment.getExternalStorageDirectory();
File myFile = new File(sdCard.getAbsolutePath() + "/rlgl");
myFile.mkdir();
// myFile.createNewFile();
String newLine = System.getProperty("line.separator");
File file = new File(myFile, "rlgls.txt");
if(file.exists()) {
} else if (!file.exists()){
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
for (int i = 0; i < 30; i++) {
myOutWriter.append("0.0" + newLine);
}
myOutWriter.close();
fOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
This is the code that I am using. I've followed directions from other Stackoverflow responses but it never goes to the SD Card. Is there something I'm doing wrong? Also a follow up question is there a way for me to use the above code in order to make the files invisible to the user. They should have no reason to open them. Thanks in advance.
It correctly creates the directory and the files, but always to the Internal Storage, never the External Storage
No, it places them on external storage. What the user sees as internal storage is what the developer sees as external storage. Internal storage is accessed via methods like getFilesDir(). And none of those are removable storage, such as some form of SD card.
Also a follow up question is there a way for me to use the above code in order to make the files invisible to the user. They should have no reason to open them.
Then put them on internal storage.
my app can't read/write from/to the files when there is a "." in front of their names
I find that very difficult to believe. The . prefix makes them not show up by default in some file browsers, but that's it. Users can get to them (if they are on external storage), and apps can get to them (subject to the same rules as any other files, those without a leading .).
I am in need to create a text file where the input is taken from the user.
I am trying to save the txt file in a folder(folder should be created even if it is not there)
My code is as follows,
FileOutputStream fileos = null;
if (FreeMemory() != 0) {
FileOutputStream fos=null;
try {
File path=new File(getFilesDir(),"sri");
if(!path.exists()){
path.mkdir();
}
File mypath=new File(path,"myfile.txt");
if (!mypath.exists()) {
fos = new FileOutputStream( mypath);
String text="Write Hii";
fos.write(text.getBytes());
fos.close();
}
}catch(Exception e){
}
}
The "path" variable gives this path: "/data/data/com.example.gm/files/sri"
I navigated to this path in my device: Android-->Data-->com.example.gm-->files-->
but the folder "sri" is not created and even the file also. Am I navigating to th ecorrect path ? I am not able to find out the file in the device.
I searched for the folder by installing "Astro File Manager" app too. But, couldn't find it. I am not getting any Exception when writing to the folder. The code must be correct. But where is the folder and file? Please anyone help me in solving this.
I want to save the txt file in internal memory of my device.
Whats wrong with my code? Please suggest me the solution.
I have gone through many trails and finally approached stackoverflow.
Thanks for any help!!
The data in the internal storage is not accessible out side the application owning it, so any third party application like file browser or Astro File Manager will not be able to see the files. The application with root access will only be able to access the files in the internal memory. I believe you should programmatically verify if the file is created. getFilesDir() will give the internal memory location allocated to that app.
Have you give the permission in manifiest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I would like to save a file on external SdCard.I have implemented an application for save a file on external sdcard.But my Android MotorolaARTIX2 device contains internal sdcard.When i am trying to save file on external sdcard it always saving to internal sdcard in my device.
I have implemented my application as follows:
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File file = new File(root, "myfile.txt");
FileWriter gpxwriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write("Hello world");
out.close();
}
} catch (IOException e) {
Log.e("Exception", "Could not write file " + e.getMessage());
}
From the above code my application always saving myfile.txt file on internal sdcard but not external sdcard-ext.And my application is support all devices with same code.
How can i save myfile.txt on sdcard-ext(external) not on sdcard(internal) in my device?
please any body help me....
Motorola has an API for this. Look here: http://developer.motorola.com/docs/motorola-external-storage-api/ But that's not a good generic solution. You probably need to scan the filesystem for a more generic solution that will work on all devices.
Take a look at the answer from this question, especially the one from Baron
I would like to make a file browser that will do two things:
1) Allow the user to browse and select a directory
2) Allow the user to browse all files on their sdcard
I've looked for tutorials but can't seem to find any?
Can someone please help me by either explaining how what my code would need to do in order to have a simple file browser or providing me with a link to a tutorial/source code?
Please and thanks!
If you're actually more interested in learning to write your own, I'd suggest taking a good long read through the File class documentation. That's where you're going to be doing most of the work.
In the case of SD cards/other external storage for Android, you'll want to first check to ensure that the external storage is mounted and available before trying to read it, using the Environment class:
String extState = Environment.getExternalStorageState();
//you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY)
//if you are only interested in reading the filesystem
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
//do your file work here
}
Once you've determined the proper state of the external storage, a simple way to start is to use File's listFiles() method, like so:
//there is also getRootDirectory(), getDataDirectory(), etc. in the docs
File sd = Environment.getExternalStorageDirectory();
//This will return an array with all the Files (directories and files)
//in the external storage folder
File[] sdDirList = sd.listFiles();
You can then start using FileFilters to narrow down your results:
FileFilter filterDirectoriesOnly = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] sdDirectories = sd.listFiles(filterDirectoriesOnly);
From there on, just read through the docs to find the type of thing you're looking to do with it, and then you can work on tying these into list adapters, etc.
Hope this helps!
This is a late answer but I worked on creating an android file explorer recently. https://github.com/mburman/Android-File-Explore
Its really straightforward. Essentially its just 1 file that you would need to integrate into your application.
Take a look at OI File Manager, which is an open-source Android file manager. You can get the source code here.