本文简单讲解一下内部存储InternalStorage的使用
InternalStorage是指直接将文件保存到系统内部存储空间.默认情况下,使用该方法保存的文件会对当前应用程序可见,对于其它应用程序,是不可见的,如果用户卸载了该应该程序,则保存数据的文件也会一起被删除。
这里的数据可以是各种文件,比如:图片,文档,视频等等,文件可大可小。但是一般不要太大,如果是下载多个大的视频,一般是下载到外部空间SD卡目录下的。
存储数据步骤如下
1)调用openFileOutput(String name,int mode)方法,该方法会返回一个 FileOutputStream; //参数1:保存的文件名,参数2:操作模式。模式一般使用:常量MODE_PRIVATE
2)使用 write()方法向文件中写入数据
3)使用flush()方法刷新输出流
4)使用close()方法关闭流
读取数据用:
openFileInput(String name)方法
下面是一个简单保存和读取内部存储器数据的示例:
程序功能:
1)点击查看是否可以使用外部存储空间。
2)保存输入框内的数据到一个文本文件中,把文本文件保存到内部存储器的根目录下。
3)读取刚才保存的文本的数据,显示到输入框的位置。
(一)xml布局文件的设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/main_et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="save" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="read"
android:text="read" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="check"
android:text="check" />
</LinearLayout>
(二)java代码设计
package com.lwz.internal;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/***
* 内部存储器的存储,没有SD卡的情况只能这样存储文件,
* 但是有SD卡也是也可以使用内部存储器来保存文件的
* 这里把文件存储在程序内部,
* 程序删除后,文件也会被删除
* 文件保存可以不指明路径,默认路径是/data/date/包名/路径下
* 但是文件名还是要定义好的。
*
*/
public class MainActivity extends AppCompatActivity {
EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_content = (EditText) findViewById(R.id.main_et_content);
}
//保存信息文件
public void save(View v) {
try {
//输出流,这里用openFileOutput获取,定义文件名和文件的存储模式
FileOutputStream fos = openFileOutput("name.txt", MODE_APPEND);
//对文件写信息
fos.write(et_content.getText().toString().getBytes());
//刷新数据并关闭流
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读取信息文件
public void read(View v) {
try {
//输出流,这里用openFileInput获取
FileInputStream fis = openFileInput("name.txt");
//对文件读信息
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
//把读取到的信息放到输入框内
et_content.append(new String(buf, 0, len));
}
//关闭流
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void check(View v) {
//获取存储器状态
String state = Environment.getExternalStorageState();
//sd卡就绪状态
if (state.equals(Environment.MEDIA_MOUNTED)) {
//外部存储器
Toast.makeText(this, "可以使用外部存储器", Toast.LENGTH_SHORT).show();
} else {
//只能使用内部存储器
Toast.makeText(this, "只能使用内部存储器", Toast.LENGTH_SHORT).show();
}
}
}
程序运行后的结果:
简单保存和读取功能基本没有问题。这里是内部存储空间,还不需要添加权限,如果使用外部存储空间SD卡是要添加用户权限的。

本文介绍Android内部存储(Internal Storage)的基本概念及使用方法,包括如何通过API保存和读取文件,如图片、文档等,并提供了具体的示例代码。
1008

被折叠的 条评论
为什么被折叠?



