黎活明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 * ...
随机推荐
- MVC案例之多个请求对应一个servlet
CustomerServlet package com.aff.mvcapp.servlet; import java.io.IOException; import java.lang.reflec ...
- STM32读取bq33100数据——硬件IIC
背景:拟采用bq33100超级电容管理芯片,实现自动的超级电容组的均压任务.需监控芯片的工作情况,以及电容组的均压情况. 平台: 硬件:STM32F103C8T6 通信:SMBus(低速IIC) 目标 ...
- jchdl - GSL值的传播
https://mp.weixin.qq.com/s/jgMljoca-Cwe9x0NaTLzZg GSL的拓扑模型是线和节点连接的模型,值的传播,即是值在线和节点之间传播和转化的过程. 值的 ...
- URL跳转与钓鱼
从登录页跳转到另一个页面就叫做URL跳转. 1.URL跳转 URL跳转一般分为两种,(1)客户端跳转:(2)服务端跳转.对用户来说,两种跳转都是透明的,都是指向或者跳转到另一个页面,页面发生了改变.但 ...
- 【HIVE】(2)分区表、二级分区、动态分区、分桶、抽样
分区表: 建表语句中添加:partitioned by (col1 string, col2 string) create table emp_pt(id int, name string, job ...
- Multiple annotations found at this line: - Undefined attribute name (charset).
把 meta charset="UTF-8" 改为 meta http-equiv="Content-Type" content="text/html ...
- 高性能可扩展mysql 笔记(一)数据库表、索引、SQL语句设计规范
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 项目说明:该笔记的背景为电商平台项目,电商项目由于其高并发.多线程.高耗能等特性,在众多的项目类型中涉及 ...
- Java实现 蓝桥杯 算法提高 快乐司机
算法提高 快乐司机 时间限制:1.0s 内存限制:256.0MB 问题描述 "嘟嘟嘟嘟嘟嘟 喇叭响 我是汽车小司机 我是小司机 我为祖国运输忙 运输忙" 这是儿歌"快乐的 ...
- Java实现 蓝桥杯VIP 算法训练 阿尔法乘积
蓝桥杯–阿尔法乘积 问题描述 计算一个整数的阿尔法乘积.对于一个整数x来说,它的阿尔法乘积是这样来计算的:如果x是一个个位数,那么它的阿尔法乘积就是它本身:否则的话,x的阿尔法乘积就等于它的各位非0的 ...
- Java实现复数运算
1 问题描述 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间的加法.减法.乘法和除法分别用不用的函数来实现. (3)必须使 ...