Android External Storage Android Storage Options In android

  • Slides: 24
Download presentation
Android External Storage

Android External Storage

Android Storage Options In android, we have different storage options to store and retrieve

Android Storage Options In android, we have different storage options to store and retrieve the application data based on our requirements such as; • Shared preferences • Internal storage • External storage • SQLite storage

 • In android, External Storage is useful to store the data files publically

• In android, External Storage is useful to store the data files publically on the shared external storage using the File. Output. Stream object. • After storing the data files on external storage, we can read the data file from external storage media using a File. Input. Stream object.

 • The data files saved in external storage are wordreadable and can be

• The data files saved in external storage are wordreadable and can be modified by the user when they enable USB mass storage to transfer files on a computer.

Grant Access to External Storage • To read or write files on the external

Grant Access to External Storage • To read or write files on the external storage, our app must acquire the WRITE_EXTERNAL_STORAGE and READ_EX TERNAL_STORAGE system permissions.

Grant Access to External Storage • To read or write files on the external

Grant Access to External Storage • To read or write files on the external storage, our app must acquire the WRITE_EXTERNAL_STORAGE and READ_EX TERNAL_STORAGE system permissions. • For that, we need to add the following permissions in the android manifest file like as shown below.

Grant Access to External Storage • To read or write files on the external

Grant Access to External Storage • To read or write files on the external storage, our app must acquire the WRITE_EXTERNAL_STORAGE and READ_EX TERNAL_STORAGE system permissions. <manifest> • For that, we need to add the following permissions in the. . android <uses- manifest file like as shown below. permission android: name="android. permission. WRITE_EXTERNAL_STOR AGE"/> <usespermission android: name="android. permission. READ_EXTERNAL_STORA GE"/>. . </manifest>

Checking External Storage Availability • Before we do any work with external storage, first

Checking External Storage Availability • Before we do any work with external storage, first we need to check whether the media is available or not by calling get. External. Storage. State(). The media might be mounted to a computer, missing, read-only or in some other state.

Checking External Storage Availability • Before we do any work with external storage, first

Checking External Storage Availability • Before we do any work with external storage, first we need to check whether the media is available or not by boolean Available= false; calling get. External. Storage. State(). The media might be boolean Readable= false; mounted to a computer, missing, read-only or in some String state = Environment. get. External. Storage. State(); other state. if(Environment. MEDIA_MOUNTED. equals(state)){ // Both Read and write operations available Available= true; } else if (Environment. MEDIA_MOUNTED_READ_ONLY. equals(state)){ // Only Read operation available Available= true; Readable= true; } else { // SD card not mounted Available = false; }

Checking External Storage Availability • Before we do any work with external storage, first

Checking External Storage Availability • Before we do any work with external storage, first we need to check whether the media is available or not by boolean Available= false; calling get. External. Storage. State(). The media might be boolean Readable= false; mounted to a computer, missing, read-only or in some String state = Environment. get. External. Storage. State(); other state. if(Environment. MEDIA_MOUNTED. equals(state)){ // Both Read and write operations available Available= true; } else if (Environment. MEDIA_MOUNTED_READ_ONLY. equals(state)){ In this code snippet, we used get. External. Storage. State() method to // Only Read operation available know the status of media mounted to a computer, such as missing, Available= true; Readable= true; read-only or in some other state. } else { // SD card not mounted Available = false; }

 • In android, by using get. External. Storage. Publish. Directory() method we can

• In android, by using get. External. Storage. Publish. Directory() method we can access the files from appropriate public directory by passing the type of directory we want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, etc. based on our requirements.

 • If we save our files to corresponding media-type public directory, the system's

• If we save our files to corresponding media-type public directory, the system's media scanner can properly categorize our files in the system. • The example to create a directory for a new photo album in the public pictures directory.

 • If we save our files to corresponding media-type public directory, the system's

• If we save our files to corresponding media-type public directory, the system's media scanner can properly categorize our files in the system. • The example to create a directory for a new photo album in the public pictures directory. // Get the directory for the user's public pictures directory. File file = new File(Environment. get. External. Storage. Public. Directory( Environment. DIRECTORY_PICTURES), album. Name); if (!file. mkdirs()) { Log. e(LOG_TAG, "Directory not created"); }

 • If we save our files to corresponding media-type public directory, the system's

• If we save our files to corresponding media-type public directory, the system's media scanner can properly categorize our files in the system. • The example to create a directory for a new photo album in the public pictures directory. // Get the directory for the user's public pictures directory. File file = new File(Environment. get. External. Storage. Public. Directory( Environment. DIRECTORY_PICTURES), album. Name); if (!file. mkdirs()) { • In case, "Directory if we are handling the files that are not intended for Log. e(LOG_TAG, not created"); } other apps to use, then we should use a private storage directory on the external storage by calling get. External. Files. Dir().

Write a File to External Storage • By using android File. Output. Stream object

Write a File to External Storage • By using android File. Output. Stream object and get. External. Storage. Public. Directory method, we can easily create and write data to the file in external storage public folders.

Write a File to External Storage • By using android File. Output. Stream object

Write a File to External Storage • By using android File. Output. Stream object and get. External. Storage. Public. Directory method, we can easily create and write data to the file in external storage public folders. • Following is the code snippet to create and write a public file in the device Downloads folder.

Write a File to External Storage • By using android File. Output. Stream object

Write a File to External Storage • By using android File. Output. Stream object and get. External. Storage. Public. Directory method, we can easily create and write data to the file in external storage public folders. • Following is the code snippet to create and write a public String = "user_details"; file in. FILENAME the device Downloads folder. String name = “vcet"; File folder = Environment. get. External. Storage. Public. Directory(Environment. DIRECT ORY_DOWNLOADS); File my. File = new File(folder, FILENAME); File. Output. Stream fstream = new File. Output. Stream(my. File); fstream. write(name. get. Bytes()); fstream. close();

Write a File to External Storage • By using android File. Output. Stream object

Write a File to External Storage • By using android File. Output. Stream object and get. External. Storage. Public. Directory method, we can easily create and write data to the file in external storage public folders. • Following is the code snippet to create and write a public String = "user_details"; file in. FILENAME the device Downloads folder. String name = “vcet"; File folder = Environment. get. External. Storage. Public. Directory(Environment. DIRECT ORY_DOWNLOADS); File my. File = new File(folder, FILENAME); • Here we are creating=and a file in device File. Output. Stream fstream newwriting File. Output. Stream(my. File); fstream. public Downloads folder by write(name. get. Bytes()); fstream. close(); using get. External. Storage. Public. Directory method.

Write a File to External Storage • By using android File. Output. Stream object

Write a File to External Storage • By using android File. Output. Stream object and get. External. Storage. Public. Directory method, we can easily create and write data to the file in external storage public folders. • Following is the code snippet to create and write a public String = "user_details"; file in. FILENAME the device Downloads folder. String name = “vcet"; File folder = Environment. get. External. Storage. Public. Directory(Environment. DIRECT ORY_DOWNLOADS); File my. File = new File(folder, FILENAME); File. Output. Stream fstream = new File. Output. Stream(my. File); fstream. write(name. get. Bytes()); fstream. close(); • We used write() method to write the data in file and used close() method to close the stream.

Read a File from External Storage • By using the android File. Input. Stream

Read a File from External Storage • By using the android File. Input. Stream object and get. External. Storage. Public. Directory method, we can easily read the file from external storage.

Read a File from External Storage • By using the android File. Input. Stream

Read a File from External Storage • By using the android File. Input. Stream object and get. External. Storage. Public. Directory method, we can easily read the file from external storage. • Following is the code snippet to read data from a file that is in the downloads folder.

Read a File from External Storage • By using the android File. Input. Stream

Read a File from External Storage • By using the android File. Input. Stream object and get. External. Storage. Public. Directory method, we can easily read the file from external storage. • Following is the code snippet to read data from a file that is String FILENAME = "user_details"; Fileinfolder = the downloads folder. Environment. get. External. Storage. Public. Directory(Environment. DIRECTORY_DOWNLOADS); File my. File = new File(folder, FILENAME); File. Input. Stream fstream = new File. Input. Stream(my. File); String. Buffer sbuffer = new String. Buffer(); int i; while ((i = fstream. read())!= -1){ Here we are reading a file from device Downloads folder storage by sbuffer. append((char)i); using File. Input. Stream object get. External. Storage. Public. Directory m } ethod with file name. fstream. close();

Read a File from External Storage • By using the android File. Input. Stream

Read a File from External Storage • By using the android File. Input. Stream object and get. External. Storage. Public. Directory method, we can easily read the file from external storage. • Following is the code snippet to read data from a file that is String FILENAME = "user_details"; Fileinfolder = the downloads folder. Environment. get. External. Storage. Public. Directory(Environment. DIRECTORY_DOWNLOADS); File my. File = new File(folder, FILENAME); File. Input. Stream fstream = new File. Input. Stream(my. File); String. Buffer sbuffer = new String. Buffer(); int i; while ((i = fstream. read())!= -1){ sbuffer. append((char)i); We used read() method to read one character at a time from the file and } used close() method to close the stream. fstream. close();