"Android" 14.2 external file storage and reading

Source: Internet
Author: User

Category: C #, Android, VS2015;

Date Created: 2016-02-27 I. INTRODUCTION

1. Basic Concepts

The private available storage space for internal storage is generally not large and should be stored on an external storage device (SD card) for larger-capacity files, such as video.

Note that the phone memory and the card inserted in the phone (mobile card, SD card) is confused, memory is not persistent storage, a shutdown all the information is lost, and internal storage (mobile dealer sold mobile phone card, capacity is small, but no it can not call), external storage (SD card, large capacity, All files can be saved) are devices that can be stored durably, so it is called file storage.

The capacity of the SD card to be created can be specified through the AOD simulator, as described in Chapter 2nd.

The following are the common folders stored on the SD card:

    • Alarms: Save audio files for warning.
    • DCIM: Save picture Files and video files (Pictures and videos) taken by the camera.
    • Downloads: Save the various types of files that users download.
    • Movies: Saves movie files that the media browser can search for.
    • Music: Saves the files that the media browser can search for.
    • Notifications: Save audio files for notifications, such as SMS notifications.
    • Pictures: Save the image file that the media browser can search.
    • Podcasts: Save the blog file.
    • Ringtones: Save your ringtone audio file.

Note: In Android 6.0, you must provide read and write access to external storage to operate on files under the SD card folder.

You can specify these folder types on the SD card by using the static properties of the Environment class.

Call the Getexternalstorgestate method to detect the status of the SD card. Before using external storage (SD card), it is best to determine whether it is available, if available, read and write directly through normal file operations.

2. How to copy files to the emulator SD card

Find C:\Users\ your user name \.android\avd\galaxy_api23.avd\ SDCARD.IMG, use the virtual optical drive software (such as UltraISO) to open this file, and then copy the file on the hard disk, paste it into the corresponding directory of the SD card, save. Second, example 2 run

After running, view the download folder on the SD card on the emulator and you will find that the A1.txt file has been written.

Third, the main design steps

1. Check read and write access to external storage

External storage requires the user to give the corresponding read and write permission to be read and written by the program.

Double-click the Properties folder in the pop-up window to see if the external storage's write permission (write_external_storage) is selected, and if unchecked, select it.

Since the example in chapter 3rd has been given permission to write, you should see that it is already selected. In addition, if the user gives the application write access to the external storage, it also means that the application is given Read permissions (read_external_storage), so the Read permission does not need to be checked.

2. Add Ch1402_main.axml File

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <ButtonAndroid:id= "@+id/mybutton"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Write file to sd card download folder" /></LinearLayout>

3. Add Ch1402MainActivity.cs File

usingAndroid.app;usingAndroid.os;usingAndroid.widget;namespacemydemos.srcdemos{[Activity (Label=""Example 14-2" External file access")]     Public classch1402mainactivity:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate);            Setcontentview (Resource.Layout.ch1402_Main); Button Button= findviewbyid<button>(Resource.Id.MyButton); button. Click+=Delegate            {                stringRootDir =Android.OS.Environment.ExternalStorageDirectory.Path; stringDownloaddir =Android.OS.Environment.DirectoryDownloads; stringFilePath = $"{rootdir}/{downloaddir}/a1.txt"; if(Android.OS.Environment.ExternalStorageState = =Android.OS.Environment.MediaMounted) {stringText ="Hello, Zhang San! ";                    System.IO.File.WriteAllText (FilePath, text, System.Text.Encoding.UTF8); Toast.maketext ( This, $"file written to: \n{filepath}", Toastlength.long).                Show ();        }            }; }    }}

"Android" 14.2 external file storage and reading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.