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
}
android之外部文件存储和读取的更多相关文章
- 【Android】14.2 外部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 1.基本概念 内部存储的私有可用存储空间一般都不会很大,对于容量比较大的文件,例如视频等,应该将其存储在外部存储设 ...
- Linux基础篇学习——Linux文件系统之文件存储与读取:inode,block,superblock
Linux文件类型 代表符号 含义 - 常规文件,即file d directory,目录文件 b block device,块设备文件,支持以"block"为单位进行随机访问 c ...
- 【Android】14.1 内部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...
- Android入门(九)文件存储与SharedPreferences存储
原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...
- .Net下二进制形式的文件存储与读取
.Net下图片的常见存储与读取凡是有以下几种:存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].1.参数是图片路径:返回 ...
- android之文件存储和读取
一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...
- android面试(4)---文件存储
1.sharePreference? SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. SharedPreferences保存数据,其背后是用xml文件存放 ...
- Android——手机内部文件存储(作业)
作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来 activity_practice2的layout文件: <?xml version="1.0" encodin ...
- android中的文件存储
Context类中提供的IO方法: FileOutputSream openFileOutput(String name, int mode):打开应用程序中的/data/data/<应用程序的 ...
随机推荐
- SQL2008 提示评估期已过的解决方法
提示窗口: 解决步骤: 第一步:进入SQL2008配置工具中的安装中心 第二步:再进入维护界面,选择版本升级 第三步:进入产品密钥,输入密钥 第四步:一直点下一步,直到升级完毕. SQL Server ...
- 10分钟制作UWP汉堡菜单
什么是汉堡菜单? 汉堡菜单,指的是一个可以弹出和收回的侧边栏.在UWP和Android应用中,汉堡菜单都非常常见. 首先我们列出所有需要掌握的前置知识: 1,SplitView 2,StackPane ...
- C++ string的大小写转换
将一个string转换成大写或者小写,是项目中经常需要做的事情,但string类里并 没有提供这个方法.自己写个函数来实现,说起来挺简单,但做起来总让人觉得不方便.打个比方:早上起来想吃个汉堡,冰箱里 ...
- 使用DateLocaleConverter和SimpleDateFormat实现字符串转换成日期
使用DateLocaleConverter: public static void change() { String birthday = "1990-12-32"; DateL ...
- 【读书笔记《Android游戏编程之从零开始》】2.Hello,World!
本人看的是PDF文档,很多都是直接都是复制粘贴的记录,简单的记录下笔记! 2.1 创建一个Android项目 Application Name: 应用名称(安装在手机上显示的名字)Project Na ...
- python3使用套接字遇到TypeError: 'str' does not support the buffer interface如何解决
这是我查看的博客 http://blog.csdn.net/chuanchuan608/article/details/17915959 直接引用里面的关键语句: When you use clien ...
- HDU 1698 & UESTC 1228 Just a hook
算是线段树中的一道水题了,必须用到懒操作,否则会超时.或者也可以刚开始不计算和,只更新节点,最后算整个线段的颜色和. 1.懒操作法 /* 908ms 3448KB in HDU OJ*/ #inclu ...
- java9-6 内部类
1. 内部类概述: 把类定义在其他类的内部,这个类就被称为内部类. 举例:在类A中定义了一个类B,类B就是内部类. 内部的访问特点: A:内部类可以直接访问外部类的成员,包括私有. B:外部类要访问内 ...
- sqlzoo.net刷题
只发后面提升题目的题解,前面的太简单,写下来也没有意义 12.查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 Find all details of the prize won by EU ...
- JS的Document属性和方法
Attributes 存储节点的属性列表(只读)childNodes 存储节点的子节点列表(只读)dataType 返回此节点的数据类型Definition 以DTD或XML模式给出的节点的定义(只读 ...