android之外部文件存储和读取
这次借用上次读写内部存储的代码,只是对将更换文件的读写路径即可。这里需要对获取SDcard的读写权限。
一、AndroidManifest.xml
这里增加了对外部存储设备的读写权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xidian.dy.com.chujia"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> </manifest>
二、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="xidian.dy.com.chujia.MainActivity"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> <EditText android:id="@+id/password" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名和密码" android:layout_centerVertical="true"/> <Button android:id="@+id/login" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录"/> </RelativeLayout> </LinearLayout>
三、MainActivity.java
package xidian.dy.com.chujia; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import java.io.BufferedReader; import java.io.BufferedWriter; 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.io.OutputStreamWriter; import java.io.PrintWriter; public class MainActivity extends AppCompatActivity { private EditText etUsername; private EditText etPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.login); etUsername = (EditText) findViewById(R.id.username); etPassword = (EditText) findViewById(R.id.password); if (bt != null) bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = null; String password = null; if (etUsername != null) username = etUsername.getText().toString(); if (etPassword != null) password = etPassword.getText().toString(); Log.e("Login", "登录成功"); CheckBox cb = (CheckBox) findViewById(R.id.remember); if (cb != null && cb.isChecked()) {
//这里获取sd卡的目录 File file = new File(Environment.getExternalStorageDirectory(), "info.txt"); PrintWriter pw; try { pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))); pw.println(username); pw.println(password); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); // Log.e("MainActivity", "file not found"); } } } }); this.readAccount(); } public void readAccount() { File file = new File(Environment.getExternalStorageDirectory(), "info.txt"); if (file.exists()) { try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String str = br.readLine(); if (etUsername != null) etUsername.setText(str); str = br.readLine(); if (etPassword != null) etPassword.setText(str); br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
四、SDcard外部存储检查
在实际开发中,我们在对SDcard进行操作的时候一定先要检查手机是否有SDcard。然后才能对SD进行读写操作。
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //TODO }