黎活明8天快速掌握android视频教程--14_把文件存放在SDCard
把文件保存在手机的内部存储空间中
1 首先必须在清单文件中添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="contract.test.savafileapplication" > <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> </manifest>
1、我们来看下业务层的代码,业务层的异常不要进行处理交给控制层activity进行处理,activity通过业务层返回的异常就知道业务是否操作成功,就可以在界面上做出相应的显示了
package contract.test.savafileapplication; import android.content.Context;
import android.os.Environment; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class FileService {
private Context context; public FileService(Context context) {
this.context = context;
} public void saveTosdCard(String fileName, String fileContext) throws Exception { File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(fileContext.getBytes());
fileOutputStream.close();
} public String readFromSdCard(String filename) throws IOException
{
File file = new File(Environment.getExternalStorageDirectory(),filename);
FileInputStream fileInputStream = new FileInputStream(file); byte[] bytes = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
/*byte是基本数据类型 如int类型
Byte是byte的包装类*/
int len = 0;
while ((len = fileInputStream.read(bytes))!=-1)
{ byteArrayOutputStream.write(bytes,0,len);
}
byteArrayOutputStream.close();
fileInputStream.close();
String str = new String(byteArrayOutputStream.toString());
return str;
}
}
activity的代码:
package contract.test.savafileapplication; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.io.IOException; public class MyActivity extends Activity {
private Button save , show;
private EditText filename, filecontext;
private TextView showContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
save = (Button)this.findViewById(R.id.saveButton);
show = (Button)this.findViewById(R.id.showButton);
filename = (EditText)this.findViewById(R.id.fileName_ET);
filecontext = (EditText)this.findViewById(R.id.saveContext_ET); showContext = (TextView)this.findViewById(R.id.showConTextView); save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = filename.getText().toString();
String context = filecontext.getText().toString();
FileService fileService = new FileService(getApplicationContext());
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
fileService.saveTosdCard(name,context);
Toast.makeText(getApplicationContext(),"文件保存成功!",Toast.LENGTH_LONG).show(); }
else
{
Toast.makeText(getApplicationContext(),"sdCard不存在或者写保护!",Toast.LENGTH_LONG).show();
} } catch (Exception e) {
Toast.makeText(getApplicationContext(),"文件保存失败!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileService fileService = new FileService(getApplicationContext());
String name = filename.getText().toString();
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
String str = fileService.readFromSdCard(name);
Toast.makeText(getApplicationContext(),"文件读取成功!",Toast.LENGTH_LONG).show();
showContext.setText(str);
}
else
{
Toast.makeText(getApplicationContext(),"sdCard不存在或者写保护!",Toast.LENGTH_LONG).show();
} } catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"文件读取失败!",Toast.LENGTH_LONG).show();
}
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入要保存的文件名:"
/>
<EditText
android:id="@+id/fileName_ET"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="请输入要保存的文件名!"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请您输入要保存的内容:"
/>
<EditText
android:id="@+id/saveContext_ET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请您在此处输入文件内容!"
/>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
/>
<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show"
/>
<TextView
android:id="@+id/showConTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
黎活明8天快速掌握android视频教程--14_把文件存放在SDCard的更多相关文章
- 黎活明8天快速掌握android视频教程--22_访问通信录中的联系人和添加联系人
Android系统中联系人的通讯录的contentProvide是一个单独的apk,显示在界面的contact也是一个独立的apk,联系人apk通过contentProvide访问底层的数据库. 现在 ...
- 黎活明8天快速掌握android视频教程--25_网络通信之资讯客户端
1 该项目的主要功能是:后台通过xml或者json格式返回后台的视频资讯,然后Android客户端界面显示出来 首先后台新建立一个java web后台 采用mvc的框架 所以的servlet都放在se ...
- 黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器
1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来 后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序 ...
- 黎活明8天快速掌握android视频教程--23_网络通信之网络图片查看器
1.首先新建立一个java web项目的工程.使用的是myeclipe开发软件 图片的下载路径是http://192.168.1.103:8080/lihuoming_23/3.png 当前手机和电脑 ...
- 黎活明8天快速掌握android视频教程--21_监听ContentProvider中数据的变化
采用ContentProvider除了可以让其他应用访问当前的app的数据之外,还有可以实现当app的数据发送变化的时候,通知注册了数据变化通知的调用者 其他所有的代码都和第20讲的一样,不同的地方看 ...
- 黎活明8天快速掌握android视频教程--20_采用ContentProvider对外共享数据
1.内容提供者是让当前的app的数据可以让其他应用访问,其他应该可以通过内容提供者访问当前app的数据库 contentProvider的主要目的是提供一个开发的接口,让其他的应该能够访问当前应用的数 ...
- 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示
1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...
- 黎活明8天快速掌握android视频教程--17_创建数据库与完成数据添删改查
1.我们首先来看下整个项目 项目也是采用mvc的框架 package dB; import android.content.Context; import android.database.sqlit ...
- 黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数
SharedPreferences保存的数据是xml格式,也是存在数据保存的下面四种权限: 我们来看看 我们来看看具体的业务操作类: /** * 文件名:SharedPrecences.java * ...
随机推荐
- [安卓基础] 004.运行app
运行你的app 这篇课程会教你: 1.如何在设备上运行你的app. 2.如何在模拟器上运行你的app. 当然,在学习之前,你还需要知道: 1.如何使用设备. 2.如何使用模拟器. 3.管理你的项目. ...
- [SD心灵鸡汤]002.每月一则 - 2015.06
1.用最多的梦面对未来 2.自己要先看得起自己,别人才会看得起你 3.一个今天胜过两个明天 4.要铭记在心:每天都是一年中最美好的日子 5.乐观者在灾祸中看到机会:悲观者在机会中看到灾祸 6.有勇气并 ...
- 使用PRTG和panabit结合定位网络阻塞的来源
一.背景 在网络管理工作中,有时会出现网络阻塞,需要定位阻塞来源以采取措施解决问题.二.以一个网络阻塞案例说明定位方法 案例:某企业日常使用多条网络线路,某一段时间发现某条线路传输速率下降,对 ...
- Rocket - tilelink - Monitor
https://mp.weixin.qq.com/s/6e-G5RSQc7Xje7mQj8-Lag 简单介绍Monitor的实现. 1. 基本介绍 用于监控各个channel上的 ...
- Chisel3 - util - Mux
https://mp.weixin.qq.com/s/TK1mHqvDpG9fbLJyNxJp-Q Mux相关电路生成器. 参考链接: https://github.com/freechips ...
- 【Win10】BeyondCompare时提示"许可证密钥已被撤销"的解决办法
删除...AppData\Roaming\Scooter Software\Beyond Compare 3目录下所有文件. 应该是对应了bcompare的配置文件以及记录文件.删除了之后,就等于新安 ...
- ASP.NET中使用Entity Framework开发登陆注册Demo
这里更多的是当作随身笔记使用,记录一下学到的知识,以便淡忘的时候能快速回顾 当前步骤是该项目的第一部分 第一部分(当前) 第二部分 大完结版本 直接上步骤,有类似的开发登陆注册也可以参考. 登陆注册的 ...
- Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
一棵10层的二叉树,最多包含多少个结点? 注意当一棵二叉树只有一个结点时为一层. 答案提交 这是一道结果填空的题,你只需要算出结果后提交即可.本题的结果为一个整数,在提交答案时只填写这个整数,填写多余 ...
- Java实现 LeetCode 76 最小覆盖子串
76. 最小覆盖子串 给你一个字符串 S.一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = ...
- java实现第五届蓝桥杯幂一矩阵
幂一矩阵 天才少年的邻居 atm 最近学习了线性代数相关的理论,他对"矩阵"这个概念特别感兴趣.矩阵中有个概念叫做幂零矩阵.对于一个方阵 M ,如果存在一个正整数 k 满足 M^k ...