Android Sacard Store data

Source: Internet
Author: User

Need to open permissions: storage.

Package Com.it.scard;

Import Java.io.BufferedReader;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.OutputStream;

Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import Android.text.TextUtils;
Import Android.text.format.Formatter;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.CheckBox;
Import Android.widget.EditText;
Import Android.widget.Toast;

public class Mainactivity extends Activity {

Private EditText Ed_qqnumber;
Private EditText Ed_qqpassword;
Private CheckBox CBX;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Initializing the control
Ed_qqnumber = (EditText) Findviewbyid (R.id.qqnumber);
Ed_qqpassword = (EditText) Findviewbyid (R.id.qqpassword);
CBX = (CheckBox) Findviewbyid (R.id.remember);

Android phone is very many, different phone OEM manufacturers changed after the SDcard name is not the same
/mnt/sdcard/emulated01
/mnt/sdcard/storage01
/mnt/sdcard/storage02
Environment.getexternalstoragedirectory ()---Dynamic return sdcard path name
File File = new file (Environment.getexternalstoragedirectory (), "info.txt");
if (File.exists () &&file.length () >0) {

Reads the data in file and then echoes back
try {
BufferedReader br = new BufferedReader (new FileReader (file));

111#itheima#111
String line = Br.readline ();

String num= Line.split ("#itheima #") [0];
String pwd = Line.split ("#itheima #") [1];
Ed_qqnumber.settext (num);
Ed_qqpassword.settext (PWD);

} catch (Exception e) {
E.printstacktrace ();
}



}

}


Login method is executed after clicking Log in
public void Login (View v) {

Get the value written in the input box
String number = Ed_qqnumber.gettext (). toString (). Trim ();
String password = Ed_qqpassword.gettext (). toString (). Trim ();

Determine if the value of number and password are filled in
if (textutils.isempty (number) | | Textutils.isempty (password)) {

Give user prompt, enter number and password
Toast.maketext (This, "Please enter QQ number and password", 0). Show ();
Return
}

Determine if the checkbox is checked, and if the checkbox is checked, then the QQ number and password will be saved.
Boolean isChecked = cbx.ischecked ();

if (isChecked) {

The description ticked

Often give users a toast (toast)

Context: Contexts
Text: The information you want to prompt the user
Duration: The duration of the message
Toast.maketext (This, "ticked", 0). Show ();

Save the data when the checkbox is checked

try {

SDcard as an external storage device, sometimes the sdcard is in a popup state, not plugged in, forced to unplug.
So in these cases, if you want to write data to SDcard, in order to ensure the exact storage of data, improve the user's feelings, so, in the storage to
SD card before, will go to dynamically determine the state of the SDcard, only in the SDcard is in the state of Mount, and then to write the SD card data

Details One:
Judging the state of Sdcar
String status = Environment.getexternalstoragestate ();

Status of the SD card that is dynamically returned here

If it is mouted---mount, then this time can write data to the SD card
if (! Environment.MEDIA_MOUNTED.equals (status)) {

Indicates that the SD card is not mounted, then the user is prompted to check the status of the SD card.

Toast.maketext (This, "Please check the status of the SD card", 0). Show ();
Return
}

Detail two:

Returns the available free space size----in bytes
Long freeSpace = Environment.getexternalstoragedirectory (). Getfreespace ();

Get the total size of the SD card, in bytes
Environment.getexternalstoragedirectory (). Gettotalspace ();

Get the size of the SD card already used, in bytes
Environment.getexternalstoragedirectory (). Getusablespace ();

Call this API to get an SD card of the available controls, here also do a thing that will return the byte space done by unit of
Transformation
String avalablesize = Formatter.formatfilesize (this, freeSpace);

Toast.maketext (This, "available space is:" + avalablesize, 0). Show ();



File File = new file (Environment.getexternalstoragedirectory (), "info.txt");
OutputStream out = new FileOutputStream (file);
String value = number+ "#itheima #" +password;

Out.write (Value.getbytes ());
Out.close ();

Toast.maketext (This, "checked, saved successfully", 0). Show ();
} catch (Exception e) {
E.printstacktrace ();
Toast.maketext (This, "checked, not saved successfully", 0). Show ();
}

}else{
Do not save the data when the checkbox is not checked
Description not checked
Toast.maketext (This, "unchecked", 0). Show ();

}

}

}

Android Sacard Store data

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.