之前我们的内容是存在手机自带的存储空间里的,但是我们知道手机自带的存储空间比较小,如果我们要存储的内容,比如说视频占空间很大,我们就必须得将大文件存入SDCard。
1.直接存储,文件存入/data/data/<package name>/files目录下
FileService.java 保存文件方法代码
1
2
3
4
5
6
7
|
//直接存储文件
FileOutputStream fos;
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
}
|
单元测试存储:
1
|
FileService service=new FileServie();
|
存储成功后的aa文件存到data/data/项目/files下的aa.txt,导出文件查看内容是:sfdgdhfh
2.存入SDCard
用户输入文件名及内容点击保存按钮存储到SDCard
布局代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
android:layout_width
=
"fill_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"文件名称"
tools:context
=
".MainActivity"
/>
<
EditText
android:id
=
"@+id/filename"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:inputType
=
"text"
/>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"文件内容"
/>
<
EditText
android:id
=
"@+id/content"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"top"
android:minLines
=
"4"
android:inputType
=
"textMultiLine"
/>
<
Button
android:id
=
"@+id/save"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"保存"
tools:context
=
".MainActivity"
/>
</
LinearLayout
>
|
编写业务类FileService,实现存储功能saveToSDCard()及读文件readFile()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
public
class
FileService {
private
Context context;
public
FileService(Context context) {
super
();
this .context = context;
}
public
void
saveToSDcard(String filename, String content)
throws
IOException{
//创建文件file及得到外部存储设备绝对路径
File file=
new
File(Environment.getExternalStorageDirectory(),filename);
//文件输出流
FileOutputStream fos=
new
FileOutputStream(file);
//通过文件输出流写入文件内容
fos.write(content.getBytes());
fos.close();
}
// 读文件
public
String readFile(String filename)
throws
IOException {
FileInputStream fis = context.openFileInput(filename);
int
len =
0
;
byte
[] buffer =
new
byte
[
1024
];
// 往内存中输出数据
//另一种输出方式 StringBuffer sbuffer=new StringBuffer();
ByteArrayOutputStream outstream =
new
ByteArrayOutputStream();
while
((len = fis.read(buffer)) != -
1
) {
//sbuffer.append(buffer);
outstream.write(buffer,
0
, len);
}
// 得到内存中数据以二进制存放
byte
[] s=outstream.toByteArray();
outstream.close();
fis.close();
return
new
String(s);
}
}
|
编写测试类测试存储方法是否有错误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public
class
FileServiceTest
extends
AndroidTestCase {
FileService service =
new
FileService(getContext());
public
void
testSaveToSDcard() {
try
{
service.saveToSDcard(
"b.txt"
,
"文件存储到SDCard,真好"
);
}
catch
(IOException e) {
e.printStackTrace();
}
}
public
void
testReadFile() {
try
{
System.out.println(service.readFile(
"b.txt"
));
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
单元测试应配置一些属性
1
2
|
<!-- 单元测试库的配置 -->
<
uses-library
android:name
=
"android.test.runner"
/>
|
1
2
3
4
5
|
<!-- 单元测试 -->
<
instrumentation
android:name
=
"android.test.InstrumentationTestRunner"
android:targetPackage
=
"cn.bzu.filefro"
>
</
instrumentation
>
|
Activity代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public
class
MainActivity
extends
Activity {
private
EditText filename;
private
EditText content;
private
Button save;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filename=(EditText)
this
.findViewById(R.id.filename);
content=(EditText)
this
.findViewById(R.id.content);
save=(Button)
this
.findViewById(R.id.save);
save.setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View v) {
String file=filename.getText().toString();
String filecontent=content.getText().toString();
FileService service= new FileService(MainActivity. this );
try
{
service.saveToSDcard(file, filecontent);
System.out.println(service.readFile(file));
Toast.makeText(MainActivity.
this
, file+
"存储成功\n内容是:"
+filecontent, Toast.LENGTH_LONG).show();
}
catch
(IOException e) {
e.printStackTrace();
}
}
});
}
}
|
效果图
文件存储到mnt/sdcard/下
导出hello内容是:
大家好,见到你们很高兴,嘿嘿。希望以后相处融洽。