手机分为内部存储和外部存储。内部存储,即在本工程目录下。外部存储在sd卡上。手机没有sd卡也能运行程序。
判断sd是否存在
<span style="white-space:pre"> </span>//MEDIA_UNKNOWN:不能识别sd卡 //MEDIA_REMOVED:没有sd卡 //MEDIA_UNMOUNTED:sd卡存在但是没有挂载 //MEDIA_CHECKING:sd卡正在准备 //MEDIA_MOUNTED:sd卡已经挂载,可用 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
package com.example.shpaerdemo01; import java.io.File; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.Toast; import com.example.shpaerdemo02.R; public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener { // 实现点击记住密码,用户登录时读取到密码 private EditText word; private EditText pass; private Button but; private CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); initword(); } private void initword() {
<span style="white-space:pre"></span><pre name="code" class="html">// 获取到存储info文件 shared = getSharedPreferences("info", MODE_PRIVATE); //1,为文件中的键.2,如果没有键默认显示为空 String names = shared.getString("word", ""); String words = shared.getString("pass", ""); if (names != null && words != null) {//当内容都不是空的时候执行 checkBox.setChecked(true); word.setText(names); pass.setText(words); }
} private void initview() { word = (EditText) findViewById(R.id.editText1); pass = (EditText) findViewById(R.id.editText2); but = (Button) findViewById(R.id.button1); cb = (CheckBox) findViewById(R.id.checkBox1); but.setOnClickListener(this); // 多选按钮的事件 cb.setOnCheckedChangeListener(this); } @Override public void onClick(View v) { String w = word.getText().toString().trim(); String p = pass.getText().toString().trim(); // 判断是否被选中 if (cb.isChecked()) { // 获取到存储文件 //1.文件名,2.文件读写模式一般都为私有 // info保存为xml文件。不需要给后缀名。 SharedPreferences shared = getSharedPreferences("info",MODE_PRIVATE); // 获取到编辑器 Editor editor = shared.edit(); // 键值对形式 editor.putString("word", w); editor.putString("pass", p); // 提交 editor.commit(); } Toast.makeText(this, "登录成功", 0).show(); } } }
//MEDIA_UNKNOWN:不能识别sd卡
//MEDIA_REMOVED:没有sd卡
//MEDIA_UNMOUNTED:sd卡存在但是没有挂载
//MEDIA_CHECKING:sd卡正在准备
//MEDIA_MOUNTED:sd卡已经挂载,可用
package com.itheima.rwinrom; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import org.apache.http.entity.InputStreamEntity; import com.itheima.rwinsd.R; import android.os.Bundle; import android.os.Environment; import android.annotation.SuppressLint; import android.app.Activity; 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 et_name; private EditText et_pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_pass = (EditText) findViewById(R.id.et_pass); readAccount(); } public void readAccount(){ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File file = new File("sdcard/info.txt"); if(file.exists()){ try { FileInputStream fis = new FileInputStream(file); //把字节流转换成字符流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); //读取txt文件里的用户名和密码 String text = br.readLine(); String[] s = text.split("**"); et_name.setText(s[0]); et_pass.setText(s[1]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void login(View v){ String name = et_name.getText().toString(); String pass = et_pass.getText().toString(); CheckBox cb = (CheckBox) findViewById(R.id.cb); //判断选框是否被勾选 if(cb.isChecked()){ // 获取sd卡路径,如果存在,证明已经挂载可用 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //返回一个File对象,其路径是sd卡的真实路径 File file = new File(Environment.getExternalStorageDirectory(), "info.txt"); // File file = new File("sdcard/info.txt"); FileOutputStream fos; try { fos = new FileOutputStream(file); fos.write((name + "**" + pass).getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ Toast.makeText(this, "sd卡不可用", 0).show(); } } Toast.makeText(this, "sd", 0).show(); } }
package com.itheima.getsdavail; import java.io.File; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.app.Activity; import android.text.format.Formatter; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize; long totalBlocks; long availableBlocks; //获取当前系统版本的等级 判断是否大于4.3版本 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){ //大于的话用新的api显示long blockSize = stat.getBlockSizeLong(); totalBlocks = stat.getBlockCountLong(); availableBlocks = stat.getAvailableBlocksLong(); } else{ //小于的话用旧的api显示int blockSize = stat.getBlockSize(); totalBlocks = stat.getBlockCount(); availableBlocks = stat.getAvailableBlocks(); } TextView tv = (TextView) findViewById(R.id.tv); //存储卡的计算方法是,每一个单位的存储的字节数*字节 tv.setText(formatSize(availableBlocks * blockSize)); } //调用系统 private String formatSize(long size) { return Formatter.formatFileSize(this, size); } }
package com.itheima.rwinrom; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import org.apache.http.entity.InputStreamEntity; import com.itheima.apirwinrom.R; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; 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 et_name; private EditText et_pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_pass = (EditText) findViewById(R.id.et_pass); readAccount(); } public void readAccount(){ //FilesDir所保存的不能系统检测到删除,cachedir目录下的当系统内存不足时,会删除。 // File file = new File(getFilesDir(), "info.txt"); File file = new File(getCacheDir(), "info.txt"); //判断文件是否存在,如果存在读取 if(file.exists()){ try { FileInputStream fis = new FileInputStream(file); //把字节流转换成字符流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); //读取txt文件里的用户名和密码 String text = br.readLine(); String[] s = text.split("##"); et_name.setText(s[0]); et_pass.setText(s[1]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void login(View v){ String name = et_name.getText().toString(); String pass = et_pass.getText().toString(); CheckBox cb = (CheckBox) findViewById(R.id.cb); //判断选框是否被勾选,勾选保存 if(cb.isChecked()){ //返回一个File对象,其路径是data/data/com.itheima.apirwinrom/files // File file = new File(getFilesDir(), "info.txt"); //返回值也是一个File对象,其路径是data/data/com.itheima.apirwinrom/cache File file = new File(getCacheDir(), "info.txt"); FileOutputStream fos; try { fos = new FileOutputStream(file); fos.write((name + "##" + pass).getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
5,这个可以直接复制粘贴当作判断sd卡是否存在的工具类
package com.xdl.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import cn.bmob.v3.datatype.BmobFile; import cn.bmob.v3.listener.UpdateListener; import cn.bmob.v3.listener.UploadFileListener; import com.xdl.bean.User; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.net.Uri; import android.os.Environment; import android.os.Message; import android.provider.MediaStore; /** * 判断SD卡是否存在 * @author Administrator * */ public class SdcardTools { public static boolean sdcard(){ String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { return true; } return false; } }