package com.example.pengtuanyuan.logindemo01;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText ed_inputName;
private EditText ed_inputPassword;
private CheckBox cb_choice;
private Button bt_login;
private static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
ed_inputName = (EditText) findViewById(R.id.ed_inputName);
ed_inputPassword = (EditText) findViewById(R.id.ed_inputPassword);
cb_choice = (CheckBox) findViewById(R.id.cb_choice);
bt_login = (Button) findViewById(R.id.bt_login);
bt_login.setOnClickListener(this);
Map<String,String> map=UserInfoUtil.getUserInfo_android(mContext);
if (map!=null){
String userName=map.get("useName");
String password=map.get("usePassword");
ed_inputName.setText(userName);
ed_inputPassword.setText(password);
cb_choice.setChecked(true);
}
File sdcard_filedir = Environment.getExternalStorageDirectory();
long usableSpace = sdcard_filedir.getUsableSpace();
long totalSpace = sdcard_filedir.getTotalSpace();
String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
if(usableSpace <1024*1024*200){
Toast.makeText(mContext,"SD card have not enough space,the rest is :"+usableSpace_str,Toast.LENGTH_SHORT).show();
return;
}
}
private void login(){
String useName = ed_inputName.getText().toString().trim();
String usePassword = ed_inputPassword.getText().toString().trim();
boolean isChecked=cb_choice.isChecked();
if (TextUtils.isEmpty(useName)||TextUtils.isEmpty(usePassword)){
Toast.makeText(mContext,"Name and password cannot be empty",Toast.LENGTH_SHORT).show();
return;
}
if (isChecked){
if(! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(mContext,"SDcard no be mounted",Toast.LENGTH_SHORT).show();
return;
}
Boolean result=UserInfoUtil.saveUserInfo_android(mContext,useName,usePassword);
if (result){
Toast.makeText(mContext,"Name and password be saved",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,"Name and password cannot be saved",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(mContext,"Name and password don't need to be saved",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_login:
if((ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {
//說明沒有動態權限需要申請
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
}else {
//說明有權限直接調用方法
login();
}
break;
default:
break;
}
}
}
----------------------------------------------------------
package com.example.pengtuanyuan.logindemo01;
import android.content.Context;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class UserInfoUtil {
public static Boolean saveUserInfo_android (Context context,String useName, String usePassword) {
try {
String userInfo=useName+"##"+usePassword;
FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
fileOutputStream.write(userInfo.getBytes());
fileOutputStream.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static Map<String,String> getUserInfo_android(Context context){
try {
FileInputStream fileInputStream = context.openFileInput("userinfo.txt");
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(fileInputStream));
String readLine =bufferedReader.readLine();
String[] split=readLine.split("##");
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put("useName",split[0]);
hashMap.put("usePassword",split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
------------------------------------------------
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
動態權限與sd卡存儲密碼
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- Android鐘對數據的存儲與訪問是很有必要的,在Android中對于數據存儲提供了如下幾種方法: 文件形式Sha...
- 直接上重點: 1:獲取內置SD卡的路徑, 但是判斷是否有效(是否掛載), 需要用到下面檢測掛載點的方法 2:枚舉系...
- 我們的app一般都會需要緩存和一些圖片的存儲,當然我們的目錄可以是自己的私有目錄,getExternalCache...
- 一、保存文件到手機內存 二、保存文件到SD卡 獲取手機sd空間的大小: 加入寫外部存儲的權限: 三、Sharedp...
- 之前出了個烏龍 接上一篇文章:Android 6.0 讀寫SD卡權限問題STEP5、最簡單的方法----push之...