Usually we need to store large files such as audio, video, etc. on our mobile phones. We have learned to use File for storage before (using File operations for storage); considering that the mobile phone itself has a small storage space, we need to store files in SDcards. Today, we also learned how to use sdcards on Android;
First of all, if you want to use sdcard to store in the program, we must set the following permissions in the file:
Add the permissions to access SDCard in the following:
<!-- existSDCardCreate and delete files permissions --> <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- PastSDCardWrite data permissions --> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
Then when we use SDcard for reading and writing, we will use several static methods below the Environment class.
1: getDataDirectory() gets the data directory in Android
2: getDownloadCacheDirectory() Get the downloaded cache directory
3: getExternalStorageDirectory() Gets the directory to external storage, generally refers to SDcard
4: getExternalStorageState() Gets the current status of external settings. Generally refers to SDcard.
For external settings in Android, we commonly use MEDIA_MOUNTED (SDcard exists and can be read and written) MEDIA_MOUNTED_READ_ONLY (SDcard exists and can only be read). Of course, there are other states that can be found in the document.
5: getRootDirectory() to get the Android Root path
6: isExternalStorageEmulated() Returns the Boolean value to determine whether the external settings are valid
7: isExternalStorageRemovable() Returns the Boolean value and determines whether the external settings can be removed
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Write with SDcard" /> <EditText android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Use SDcard read operation" /> </LinearLayout>
package ;
import ; import ; import .; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private Button bt1, bt2; private EditText et1, et2; private static final String FILENAME = "temp_file.txt"; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); bt1 = (Button) (.bt1); bt2 = (Button) (.bt2); et1 = (EditText) (.et1); et2 = (EditText) (.et2); (new MySetOnClickListener()); (new MySetOnClickListener()); } private class MySetOnClickListener implements OnClickListener { @Override public void onClick(View v) { File file = new File((), FILENAME); switch (()) { case .bt1:// Write with SDcardif (().equals( Environment.MEDIA_MOUNTED)) { try { FileOutputStream fos = new FileOutputStream(file); (().toString().getBytes()); (); (, "Writing to the file successfully", Toast.LENGTH_LONG).show(); } catch (Exception e) { (, "Write to file failed", Toast.LENGTH_SHORT).show(); } } else { // At this time, SDcard does not exist or cannot perform read and write operations.(, "The SDcard does not exist or cannot perform read and write operations", Toast.LENGTH_SHORT).show(); } break; case .bt2:// Use SDcard to readif (().equals( Environment.MEDIA_MOUNTED)) { try { FileInputStream inputStream = new FileInputStream(file); byte[] b = new byte[()]; (b); (new String(b)); (, "Reading the file successfully", Toast.LENGTH_LONG).show(); } catch (Exception e) { (, "Read failed", Toast.LENGTH_SHORT).show(); } } else { // At this time, SDcard does not exist or cannot perform read and write operations.(, "The SDcard does not exist or cannot perform read and write operations", Toast.LENGTH_SHORT).show(); } break; } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in . int id = (); //noinspection SimplifiableIfStatement if (id == .action_settings) { return true; } return (item); } }
The above description introduces you to using SDcard to read files in Android, hoping it will be helpful to everyone!