File externalStorageDirectory = Environment.getExternalStorageDirectory(); String path = externalStorageDirectory.getAbsolutePath(); Log.d("External Storage Path", path);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File externalDir = Environment.getExternalStorageDirectory(); if(!externalDir.exists()) { externalDir.mkdirs(); } File file = new File(externalDir, "example.txt"); if(!file.exists()) { file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); fos.write("Hello world".getBytes()); fos.close(); }This example first checks if the external storage is available and mounted, creates a new file in the primary external storage directory, writes some text to the file, and closes the output stream. The package library for the Environment class is android.os.