How Android operates SD card

Source: Internet
Author: User

Affirm Sharedpreferences Object

Sharedpreferences msharedpreferences;
Sharedpreferences.editor Meditor;
Msharedpreferences = getsharedpreferences ("user", context.mode_private);
Meditor = Msharedpreferences.edit ();

Write a click event in the Main method to save the data
Button left = (Button) Findviewbyid (r.id.left);
Left.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Writetosd ();
Meditor.putstring ("User", Edittext1.gettext (). toString ());
Meditor.putstring ("Passworld", Edittext2.gettext (). toString ());
Meditor.commit ();
}
});

user = msharedpreferences.getstring ("user", null);
Passworld = msharedpreferences.getstring ("Passworld", null);
Edittext1.settext (user);
Edittext2.settext (Passworld);



Read file
private void Readfromsd () {
String s = environment.getexternalstoragestate ();
File root = Environment.getexternalstoragedirectory ();
File TargetDir = new file (Root,super.getpackagename ());
ObjectInputStream ois = null;
if (targetdir.exists ()) {
try {
OIS = new ObjectInputStream (new FileInputStream (New File (targetdir+ "/liu.txt"));
People people = (people) Ois.readobject ();
Toast.maketext (Getapplication (), People.getuser (). ToString () +people.getpwd (). ToString (), Toast.length_short). Show ();
} catch (IOException e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}finally {
try {
Ois.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}else {
Toast.maketext (Getapplication (), "false", Toast.length_short). Show ();
}
}
Write file
private void Writetosd () {
String s = environment.getexternalstoragestate ();
ObjectOutputStream oos = null;
if (S.equals (environment.media_mounted)) {//To determine whether there is permission, remember to declare the permissions in the manifest file, the phone after the 6.1 version may not be
File root = Environment.getexternalstoragedirectory ();
File tag = new file (root, Super.getpackagename ());
if (!tag.exists ()) {//Determine if the file exists
            Tag.mkdir ();
}
try {
Oos = new ObjectOutputStream (new FileOutputStream (tag, "liu,txt"));
People people = New People ("Liu", "Yifan");
Oos.writeobject (people);
} catch (IOException e) {
E.printstacktrace ();
}
}



<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.jredu.liuyifan.fragmentapplication" >//written in this area.
Two permissions
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

How Android operates SD card

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.