一、文件存储

文件存储
Android中的文件存储分为内部存储和外部存储。
内部存储:将应用程序的数据以文件方式存储到设备的内部(data/【你的APP的包名】/files下),当创建的应用程序被卸载时,其内部存储文件也随之被删除。
外部存储:是将文件存储到一些外部设备上,例如SD卡或者设备内嵌的存储卡,属于永久性的存储方式。
(一)内部存储
获取内部存储路径的几种方法:

内部存储IO流获取方法:

内部存储存文件

内部存储取文件

写入、读入的四种模式

(二)外部存储
获取外部存储路径
File file = Environment.getExternalStorageDirectory()
返回外部存储根目录
外部存储IO流获取方法
使用new关键字创建IO流对象
外部存储创建文件

外部存储存文件

外部存储取文件

静态权限申请
静态权限申请的方式适用于 Android 6.0 以下的版本,这种方式是在
AndroidManifest.xml 文件中对所需权限进行声明。申请外部存储读写权限如下所示。

动态权限申请
Android 6.0 及以上版本的系统中,Android 将权限分为正常权限和危险权限。正常权限表示不会直接给用于隐私权带来风险的权限,比如请求网络的权限。
危险权限表示涉及用户隐私的权限,申请该权限的应用可能涉及了用户隐私数据,也可能对用户存储的数据或其他应用的操作产生影响。危险权限一共分为九
组,分别为存储(STORAGE) 、照相机(CAMERA)、位置(LOCATION)、日历(CALENDAR)、
联系人(CONTACTS)、传感器(SENSORS)、麦克风(MICROPHONE)、电话(PHONE)、短
信(SMS)的相关权限共 24 个。
申请正常权限时使用静态权限申请即可,而对于危险权限则需要用户授权后才可以使用,因此不仅需要在 AndroidManifest.xml 文件中添加权限,还需要在
代码中进行动态权限申请。
注意:
①外部存储中,Environment的该getExternalStorageState()方法,已被弃用,虽然还可以使用,但是为了用户隐私安全,可以用Context的
getExternalFilesDir(null);方法替代。
②内部存储文件随着应用程序的卸载会被删掉;
外部存储为永久性存储。
二、列表控件ListView和适配器的用法
一个ListView通常有两个职责:
(1)将数据填充到布局
(2)处理用户的选择点击等操作
一个ListView创建需要3个元素:
(1)ListView中每一列的View;(2)填入View的数据或图片等;(3)链接数据与ListView的适配器。
创建适配器的作用:
**适配器是一个链接数据和AdapterView(ListView就是一个典型的AdapterView)的桥梁,**通过它能够有效地实现数据与 AdapterView的分离设置,使AdapterView与数据的绑定更加简便,修改更加方便。这也是Android完全遵循MVC设计模式的体现。
Android提供很多的Adapter,列出常用几个:
ArrayAdapter(T)--------------------------来绑定一个数组,支持泛型操作
SimpleAdapter----------------------------用来绑定在XML中定义的控件对应的数据
SimpleCursorAdapter-------------------用来绑定游标得到的数据
BaseAdapter-----------------------------通用的基础适配器
1.ListView使用SimpleAdapter
实现如图 所示的行星列表效果

(1)activity_list_view_test_activty.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListViewTestActivty">
<ListView
android:id="@+id/listViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
(2)导入案例所需图片。
在 res 目录中,创建 drawable-xxhdpi 文件夹,将所需图片放入该文件夹下。或者放入mipmap文件夹下也是可以的
(3)编写列表项布局(线性布局、相对布局)
最终实现效果的列表项中包含图片和文字,需要自定义列表项布局。此处使用了相对布局
item_listviewset.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#ff0000"
android:layout_toRightOf="@id/iv_icon"
/>
<TextView
android:id="@+id/introduces"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_name"
android:layout_alignLeft="@id/tv_name"
android:textSize="14dp"
/>
</RelativeLayout>
</LinearLayout>
(4)编写java代码
在 ListViewTestActivty.java 中编写功能逻辑代码
public class ListViewTestActivty extends AppCompatActivity {
private String[] datas = new String[]{"智慧学院", "机电学院", "教育学院", "艺术学院"};
private ListView listViewTest;
private int[] iconArray =
{R.mipmap.shuixing, R.mipmap.jinxing, R.mipmap.diqiu,
R.mipmap.huoxing, R.mipmap.muxing, R.mipmap.tuxing};
private String[] starArray={"水星", "金星", "地球", "火星", "木星", "土星"};
private String[] str= {"信息1","信息2","信息3","信息4","信息5","信息6"};
private ArrayList dataed;
private HashMap listData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_test_activty);
listViewTest=findViewById(R.id.listViewTest);
dataed=new ArrayList();
//存放数据,创建hashmap集合
for(int i=0;i<iconArray.length;i++){
listData=new HashMap();
listData.put("image",iconArray[i]);
listData.put("name",starArray[i]);
listData.put("introduces",str[i]);
//存放的是多个map集合
dataed.add(listData);
}
//2、创建SimpleAdapter适配器对象
SimpleAdapter simpleAdapter=new SimpleAdapter(this,dataed,R.layout.item_listviewset,new String[]{"image","name","introduces"},new int[]{R.id.iv_icon,R.id.tv_name,R.id.introduces});
//创建ArrayAdapter适配器对象
// ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item_listviewset, datas);
//adapter.setDropDownViewResource(R.layout.activity_setListViewTest);
//3、设置适配器
listViewTest.setAdapter(simpleAdapter);
listViewTest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(ListViewTestActivty.this,"您当前点击的是:"+starArray[i], Toast.LENGTH_LONG).show();
}
});
}
}
以上代码可以实现一个简单的列表项,包含文字和图片
运行效果如下图:

本文详细介绍了Android的文件存储,包括内部存储和外部存储的使用方法,以及静态和动态权限申请。此外,还讲解了ListView控件及适配器的工作原理,特别是如何使用SimpleAdapter实现列表展示,并给出了具体的代码示例。
2914

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



