Android保存文件到手机
16lz
2021-12-04
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="28dp" android:layout_marginTop="26dp" android:text="请输入用户名:" /> <EditText android:id="@+id/et_userName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="29dp" android:ems="10" android:inputType="textPersonName" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_userName" android:layout_below="@+id/et_userName" android:layout_marginTop="35dp" android:text="请输入密码:" /> <EditText android:id="@+id/et_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/textView2" android:layout_marginTop="56dp" android:ems="10" android:inputType="textPassword" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_pwd" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:text="记住密码" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/et_pwd" android:layout_alignTop="@+id/checkBox1" android:text="登录" android:onClick="login" /></RelativeLayout>
package com.demo.iotest;import java.io.IOException;import java.util.Map;import com.demo.loginService.LoginService;import android.os.Bundle;import android.app.Activity;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private static final String TAG = "MainActivity";private EditText et_userName;private EditText et_pwd;private CheckBox checkBox;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_userName = (EditText) findViewById(R.id.et_userName);et_pwd = (EditText)findViewById(R.id.et_pwd);checkBox = (CheckBox) findViewById(R.id.checkBox1);//检查是否有保存的用户名和密码数组,如果有,回显Map<String, String> map;try {map = LoginService.getSavedUserInfo(this);if(map != null){et_userName.setText(map.get("userName"));et_pwd.setText(map.get("pwd"));}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void login(View view){String userName = et_userName.getText().toString().trim();String pwd = et_pwd.getText().toString().trim();if(TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)){Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();}else{//登陆//判断是否保存密码if(checkBox.isChecked()){//保存用户名密码Log.i(TAG, "保存用户名密码");try {boolean result = LoginService.saveUserInfo(this,userName, pwd);if(result){Toast.makeText(this, "保存用户信息成功", 0).show();}else{Toast.makeText(this, "保存用户信息失败", 0).show();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//登陆,发送消息到服务器,服务器验证是否正确if("zhangsan".equals(userName) && "123".equals(pwd)){Toast.makeText(this, "登录成功", 0).show();}else{Toast.makeText(this, "登录失败,用户名或者密码错误", 0).show();}}}}
LoginService.java
package com.demo.loginService;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.content.Context;public class LoginService {/** * 保存用户名密码的业务方法 * @param userName 用户名 * @param context 上下文 * @param pwd 密码 * @return true 保存成功 false 保存失败 * @throws IOException * * getFileDir /data/data/包名/files * getCacheDir /data/data/包名/cache */public static boolean saveUserInfo(Context context,String userName,String pwd) throws IOException{try {//context.getFilesDir();//帮助返回一个目录/data/data/报名/files//File file = new File("/data/data/com.demo.iotest/info.txt");File file = new File(context.getFilesDir(),"info.txt");FileOutputStream fos = new FileOutputStream(file);fos.write((userName + "##" + pwd).getBytes());fos.close();return true;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;} }public static Map<String,String> getSavedUserInfo(Context context) throws Exception{File file = new File(context.getFilesDir(),"info.txt");try {FileInputStream fis = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String str = br.readLine();String[] info = str.split("##");Map<String, String> map = new HashMap<String, String>();map.put("userName", info[0]);map.put("pwd", info[1]);return map;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}}
更多相关文章
- android 获取网络图片
- Android(安卓)自定义控件之图片裁剪
- 登录界面(Android(安卓)studio)
- android 播放视频保存的一些网页
- android > android 客户端 ,PHP 服务器端 HttpGet类和HttpPost类
- adb connect 失败时(unable to connect to)解决办法
- Android的关键的持久数据应该在onPause()方法中保存
- android 绘图
- Android学习中遇到的问题及解决方案