Android Development: SD card operation

Source: Internet
Author: User

1. Write the data to the SDcard:

Found to write data to the/mnt/sdcard path error, cause: The appropriate permissions are not turned on

FIX: Add in config file

    <android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Or:

In addition: Do not recommend hard-coded written:/mnt/sdcard, should be written like this:

            String Sdpath = Environment.getexternalstoragedirectory (). GetPath ();

Before you save the data to SDcard, you should also determine if there is a sdcard:

                if (Environment.MEDIA_MOUNTED.equals (Environment                        . Getexternalstoragestate ())) {                    =  Environment.getexternalstoragedirectory ()                            . GetPath ();                     //  ......                Else {                    "SD card is not available",                            Toast.length_long). Show ();                }

2. Get sdcard total size and free space:

        File File = environment.getexternalstoragedirectory ();         // Total size        long totalspace = file.gettotalspace ();         // Available Sizes        long usablespace = file.getusablespace ();         // Note The return format is byte, and the conversion        String formattotalspace = Formatter.formatfilesize (mainactivity.  this, totalspace);         = Formatter.formatfilesize (mainactivity.  this, usablespace);

Android Development: SD card operation

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.