数据存储——手机内部文件存储2

assets目录

一.创建

1./src/main/assets

2.切换到Project视图模式,在main下新建文件夹assets

二.特点

1.和res同等级别

2.主要是存放应用中的大文件

3.文件不受R类的管理

三.API

1.AssetManager  资产管理器

①getAssets  得到资产管理器

②open(文件名) 返回文件的nputStream

2.ImageView

setImageBitmap(Bitmap  实例)  设置图片视图的位图

3.Bitmap  位图

BitmapFactory.decodeFile(图片文件路径)  使用工厂方法得到图片文件的Bitmap

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.MainActivity"
11     android:orientation="vertical">
12 
13     <TextView
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="Hello World!"
17         android:id="@+id/tv_1"
18         android:visibility="gone"/>
19     
20     <Button
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="SP存储"
24         android:onClick="bt_OnClick"
25         android:visibility="gone"/>
26 
27     <Button
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content"
30         android:text="SP读取"
31         android:onClick="bt1_OnClick"/>
32 
33     <EditText
34         android:layout_width="match_parent"
35         android:layout_height="wrap_content"
36         android:hint="输入..."
37         android:id="@+id/et_1"/>
38 
39     <Button
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content"
42         android:text="写内部文件"
43         android:onClick="bt2_OnClick"/>
44 
45     <Button
46         android:layout_width="match_parent"
47         android:layout_height="wrap_content"
48         android:text="读内部文件"
49         android:onClick="bt3_OnClick"/>
50 
51     <Button
52         android:layout_width="match_parent"
53         android:layout_height="wrap_content"
54         android:text="保存资产文件到内部存储"
55         android:onClick="bt4_OnClick"/>
56 
57     <ImageView
58         android:layout_width="wrap_content"
59         android:layout_height="50dp"
60         android:id="@+id/iv_1"
61         android:src="@drawable/f4"/>
62     <Button
63         android:layout_width="match_parent"
64         android:layout_height="wrap_content"
65         android:text="设置图片指向内部存储"
66         android:onClick="bt5_OnClick"/>
67 </LinearLayout>
  1 package com.hanqi.testapp3;
  2 
  3 import android.content.SharedPreferences;
  4 import android.content.res.AssetManager;
  5 import android.graphics.Bitmap;
  6 import android.graphics.BitmapFactory;
  7 import android.os.Bundle;
  8 import android.os.Environment;
  9 import android.support.v7.app.AppCompatActivity;
 10 import android.view.View;
 11 import android.widget.EditText;
 12 import android.widget.ImageView;
 13 import android.widget.TextView;
 14 import android.widget.Toast;
 15 
 16 import java.io.File;
 17 import java.io.FileInputStream;
 18 import java.io.FileOutputStream;
 19 import java.io.InputStream;
 20 import java.io.PrintStream;
 21 
 22 public class MainActivity extends AppCompatActivity {
 23 
 24     EditText et_1;
 25     TextView tv_1;
 26     ImageView iv_1;
 27 
 28     @Override
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.activity_main);
 32 
 33         et_1=(EditText)findViewById(R.id.et_1);
 34         tv_1=(TextView)findViewById(R.id.tv_1);
 35         iv_1=(ImageView)findViewById(R.id.iv_1);
 36     }
 37 
 38     public void bt_OnClick(View v)
 39     {
 40         //1.得到SharedPreferences对象
 41         SharedPreferences sharedPreferences=getSharedPreferences("abc",MODE_APPEND);
 42 
 43         //2.得到编辑器
 44         SharedPreferences.Editor editor=sharedPreferences.edit();
 45 
 46         //3.使用editor添加数据
 47 //        editor.putString("b","xxxxxx");
 48 //        editor.putLong("long",123456);
 49 
 50         editor.remove("a");
 51 
 52 
 53         //4.提交保存
 54         editor.commit();
 55 
 56         Toast.makeText(MainActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
 57     }
 58 
 59     //读取
 60     public void bt1_OnClick(View v)
 61     {
 62         SharedPreferences sp=getSharedPreferences("abc",MODE_PRIVATE);
 63 
 64         String str=sp.getString("ab", "默认值");
 65 
 66         Toast.makeText(MainActivity.this, "key=b"+"value="+str, Toast.LENGTH_SHORT).show();
 67     }
 68 
 69     //写内部文件
 70     public void bt2_OnClick(View v)
 71     {
 72         //从内存里写入文件
 73 
 74         //1.得到内部的存储目录
 75         try {
 76 
 77 
 78         File file=getFilesDir();
 79 
 80         String path=file.getAbsolutePath();
 81 
 82         Toast.makeText(MainActivity.this, "path"+path, Toast.LENGTH_SHORT).show();
 83 
 84         //2.用输出流写入文件
 85 
 86         FileOutputStream fos = openFileOutput("test.txt",MODE_APPEND);
 87 
 88         //3.写入文件内容
 89             PrintStream ps=new PrintStream(fos);
 90 
 91             String str=et_1.getText().toString();
 92 
 93             ps.println(str);
 94             //ps.println("自动换行");
 95             ps.close();
 96             fos.close();
 97             Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
 98         }
 99         catch (Exception e)
100         {
101             Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
102         }
103 
104 
105     }
106 
107     //
108     public void bt3_OnClick(View v)
109     {
110         try {
111             //输入流
112             FileInputStream fis = openFileInput("test.txt");
113 
114             //1.定义byte[]
115             byte[]b=new byte[1024];
116             int i=0;//读到的数据长度
117 
118             String str1="";
119 
120             //2.循环读
121             while((i=fis.read(b))>0)
122             {
123                 String str=new String(b,0,i);
124 
125                 str1+=str;
126             }
127 
128             fis.close();
129 
130             tv_1.setText(str1);
131 
132         }
133         catch (Exception e)
134         {
135 
136         }
137     }
138 
139     public void bt4_OnClick(View v)
140     {
141         try {
142             //操作assets目录的文件
143 
144             //1.得到assetsManager
145 
146             AssetManager am = getAssets();
147 
148             //2.操作资产目录,边读边写入
149             //1)读文件到内存 inputstream
150             InputStream is = am.open("f1.jpg");
151 
152 
153             //2)写文件到目录 outputstream
154             FileOutputStream fos = openFileOutput("test.jpg",MODE_PRIVATE);
155 
156             //先读后写
157             byte[]b=new byte[1024];
158             int i=0;
159 
160             while ((i=is.read(b))>0)
161             {
162               fos.write(b,0,i);
163             }
164 
165             fos.close();
166             is.close();
167 
168             Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
169 
170 
171 
172         }
173         catch (Exception ex)
174         {
175             Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
176         }
177 
178     }
179 
180     //设置图片指向内部存储
181     public void bt5_OnClick(View v)
182     {
183         //得到文件路径
184         String path=getFilesDir().getAbsolutePath()+"/test.jpg";
185 
186         Toast.makeText(MainActivity.this, "path="+path, Toast.LENGTH_LONG).show();
187 
188         //从内部存储的图片得到 Bitmap,BitmapFactory.decodeFile("文件路径");
189         Bitmap bm= BitmapFactory.decodeFile(path);
190 
191         //设置图片视图的图片来源
192         iv_1.setImageBitmap(bm);
193     }
194   }
195 }

原文地址:https://www.cnblogs.com/cycanfly/p/5535266.html