Android: how to write a file to internal storage
Use the below code to write a file to internal storage:
public void writeFileOnInternalStorage(Context mcoContext, String sFileName, String sBody){
File dir = new File(mcoContext.getFilesDir(), "mydir");
if(!dir.exists()){
dir.mkdir();
}
try {
File gpxfile = new File(dir, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
} catch (Exception e){
e.printStackTrace();
}
}
Starting in API 19, you must ask for permission to write to storage.
You can add read and write permissions by adding the following code to AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can prompt the user for read/write permissions using:
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);
and then you can handle the result of the permission request in onRequestPermissionsResult()
inside activity called from it.
how to create a folder and file in internal storage using kotlin
ok got it,not specifying proper path.
val data: String = "om namah shivaya"
val path = this.getExternalFilesDir(null)
val folder = File(path, "avalakki")
folder.mkdirs()
println(folder.exists()) // u'll get true
val file = File(folder, "file_name.txt")
file.appendText("$data")
then to check this, navigate to
Android -> data -> com.your.pkg_name -> files ->
There will see files got created.
note:- we can use different paths
val path = this.externalCacheDir
Android -> data -> com.your.pkg_name -> cache ->
val path = this.externalMediaDirs.first()
Android -> media
val path = this.getExternalFilesDirs(null).first()
val path = Environment.getExternalStorageDirectory().getPath()
print and check what the path is.
How could I create a file in the Internal Storage Directory?
After a while later I found the answer to this question.
public void createDirectory() {
File file = new File(Environment.getExternalStorageDirectory(), "/test");
if (!file.exists()) {
file.mkdirs();
Log.w("DEBUG", "Created default directory.");
}
}
This is how you create it code wise. The reason it wasn't creating was due to Samsungs weird permissions.
Make sure you have the storage permission enabled in Settings -> Apps -> App Name -> Permissions. I needed to turn it on so it would create the folder.
Related Topics
Get All Child Views Inside Linearlayout at Once
How to Make an Android Editview 'Done' Button and Hide the Keyboard When Clicked
Voice Detection in Android Application
Android - Emulator in Landscape Mode, Screen Does Not Rotate
Determine When a Viewpager Changes Pages
How to Scroll to Bottom in a Scrollview on Activity Startup
How to Check If Facebook Is Installed Android
Livedata Prevent Receive the Last Value When Start Observing
Android: Automatically Choose Debug/Release Maps V2 API Key
How to Get Current Sim Card Number in Android
Get Battery Level Before Broadcast Receiver Responds for Intent.Action_Battery_Changed