This article shows how to make an application that can write to external storage of android device.
First off all we create a new project in Eclipse, say WriteFile.
Now, we will use the method getExternalStorageState() of Environment class to get detail about the external storage state. It will return a string showing the state of external device.
We check if it is anything else than Environment.MEDIA_MOUNTED then we terminate the app with appropriate message else we continue.
Now, we know we have external storage and it also has read/write permissions. Next is to get a File object referring to the external director. Now depending on the device this may vary. So we use getExternalStorageDirectory() method to get the File object and save reference in sdcard variable.
Now we want to create a file, say a dummy.txt in the directory. To do this first obtain a reference to that file using File(String name) constructor. Then we use character streams to write data to file (We can also use the Byte Streams). Create a FileWriter to write data to file and a FileReader to read data from the file. In both constructors pass the File object of our target file. Write() method of FileWriter is used to write data to file.
Now the data from file is in buf array. We can use it the way we want now.
The Full Source code is available here .
Notes : Creating File object generates FileNotFoundExceptiion and FileReader and FileWriter Both generates IOExcption, so be carefull to surround the statements with try/catch block.
Writing to external storage requires the WRITE_EXTERNAL_STORAGE permission. In a future platform release, access to this path will require the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.
Now, we will use the method getExternalStorageState() of Environment class to get detail about the external storage state. It will return a string showing the state of external device.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String status = Environment.getExternalStorageState();
}
}
String status = Environment.getExternalStorageState();
if(!status.equals(Environment.MEDIA_MOUNTED))
return;
File sdcard = Environment.getExternalStorageDirectory();
Now we want to create a file, say a dummy.txt in the directory. To do this first obtain a reference to that file using File(String name) constructor. Then we use character streams to write data to file (We can also use the Byte Streams). Create a FileWriter to write data to file and a FileReader to read data from the file. In both constructors pass the File object of our target file. Write() method of FileWriter is used to write data to file.
char [] buf=null;
File newfile = new File(sdcard.getAbsolutePath()+"/dummy.txt");
FileWriter fileWriter = new FileWriter(newfile);
fileWriter.write("This is a demo");
FileReader fileReader = new FileReader(newfile);
buf = new char[50];
fileReader.read(buf);
Notes : Creating File object generates FileNotFoundExceptiion and FileReader and FileWriter Both generates IOExcption, so be carefull to surround the statements with try/catch block.
Writing to external storage requires the WRITE_EXTERNAL_STORAGE permission. In a future platform release, access to this path will require the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.
Ads :
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.com | www.ebay.ch | www.ebay.com.my
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.com | www.ebay.ch | www.ebay.com.my
No comments:
Post a Comment