SDcard data read and write in Android

Source: Internet
Author: User

Import Java.io.filenotfoundexception;import java.io.fileoutputstream;import java.io.ioexception;import Android. R.integer;import Android.content.context;import Android.os.environment;public class FileService {private Context cont    Ext    Public Fileservice (Context context) {This.context = context; Public Fileservice () {} public String Getfilefromsdcard (string fileName) {FileInputStream InputStream        = NULL;        Cached streams, and disk-independent, do not need to close bytearrayoutputstream outputstream = new Bytearrayoutputstream ();        File File = new file (Environment.getexternalstoragedirectory (), fileName);                if (Environment.MEDIA_MOUNTED.equals (environment. Getexternalstoragestate ())) {try {                InputStream = new FileInputStream (file);                int len = 0;                byte[] data = new byte[1024]; while (len = inputstream.read (data))! =-1) {outputstream.write (data, 0, Len); }} catch (FileNotFoundException e) {//TODO auto-generated catch block e.prints            Tacktrace ();            } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } finally {if (InputStream! = null) {try {inputstream.close (                    ); } catch (IOException e) {//TODO auto-generated catch block E.printstacktra                    CE ();    }}}} return new String (Outputstream.tobytearray ());    }/** * @param filename * The name of the file * @param content * File contents * @return */        public boolean Savecontenttosdcard (string fileName, String content) {Boolean flag = false;        FileOutputStream fileoutputstream = null; Get the path to the sdcard card file File = new file (EnviRonment.getexternalstoragedirectory (), fileName);            Determine if the SDcard card is available if (Environment.MEDIA_MOUNTED.equals (environment. Getexternalstoragestate ())) {                try {fileoutputstream = new FileOutputStream (file);                Fileoutputstream.write (Content.getbytes ());            Flag = true;            } catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();             } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } finally {if (FileOutputStream! = null) {try {fil                    Eoutputstream.close (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktra                    CE ();    }}}} return flag; }}


Manifest file

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


Related Article

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.