Android——课堂整理:assets目录和手机外部存储

layout文件:

 1 <Button
 2         android:layout_width="match_parent"
 3         android:layout_height="wrap_content"
 4         android:text="保存资产文件到内部存储"
 5         android:onClick="bt4_onClick"/>
 6     <ImageView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:id="@+id/iv_1"
10         android:src="@drawable/on"/>
11     <Button
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:text="设置图片指向内部存储"
15         android:onClick="bt5_onClick"/>
16     <Button
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:text="写入外部存储文件"
20         android:onClick="bt6_onClick"/>
21     <Button
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content"
24         android:text="读取外部存储文件"
25         android:onClick="bt7_onClick"/>

java类:

  1 //保存资产文件到内部存储
  2     public void bt4_onClick(View v)
  3     {
  4         try {
  5             //操作assets目录的文件
  6             //1.得到assetsManager
  7             AssetManager am = getAssets();
  8             //2.操作资产目录,边读边写入
  9             //1)读文件到内存 inputstream
 10             InputStream is = am.open("yuantu.png");
 11             //2)写文件到目录 outputstream
 12             FileOutputStream fos = openFileOutput("test.png",MODE_PRIVATE);
 13             //先读后写
 14             byte[] b = new byte[1024];
 15             int i = 0;
 16             while ((i = is.read(b))>0)
 17             {
 18                 fos.write(b,0,i);
 19             }
 20             fos.close();
 21             is.close();
 22             Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
 23         }
 24         catch (Exception e)
 25         {
 26             Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
 27         }
 28 
 29     }
 30     //设置图片指向内部存储
 31     public void bt5_onClick(View v)
 32     {
 33         //1.得到文件路径
 34         String path = getFilesDir().getAbsolutePath()+"/test.png";
 35         Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_SHORT).show();
 36         //2.从内部存储的图片得到Bitmap,BitmapFactory.decodeFile("文件路径");
 37         Bitmap bm = BitmapFactory.decodeFile(path);
 38         //3.设置图片视图的图片来源
 39         iv_1.setImageBitmap(bm);
 40     }
 41     //写入外部存储文件
 42     public void bt6_onClick(View v)
 43     {
 44         //1.判断SD卡是否挂载
 45         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
 46         {
 47             //得到文本框内容
 48             String str = et_1.getText().toString();
 49             try {
 50                 //写入
 51                 //1.构造输出流
 52                 //1)得到文件路径
 53                 //得到SD卡根目录
 54                 String path = Environment.getExternalStorageDirectory().getAbsolutePath();
 55 
 56                 //得到包名对应的目录
 57 //                String path = getExternalFilesDir("Music").getCanonicalPath();
 58                 Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_LONG).show();
 59                 //2)构造
 60                 FileOutputStream fos = new FileOutputStream(path+"/test.txt");
 61                 PrintStream ps = new PrintStream(fos);
 62                 ps.print(str);
 63                 ps.close();
 64                 fos.close();
 65                 Toast.makeText(MainActivity.this, "写入外部文件成功", Toast.LENGTH_SHORT).show();
 66             }
 67            catch (Exception e)
 68            {
 69                Toast.makeText(MainActivity.this, "存储文件出错", Toast.LENGTH_SHORT).show();
 70            }
 71         }
 72         else
 73         {
 74             Toast.makeText(MainActivity.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
 75         }
 76     }
 77     //读取外部存储文件
 78     public void bt7_onClick(View v)
 79     {
 80         //1.判断SD卡是否挂载
 81         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
 82         {
 83             try {
 84                 String path = getExternalFilesDir("Music").getCanonicalPath()+"/test.txt";
 85                 FileInputStream fis = new FileInputStream(path);
 86                 byte[] b = new byte[1024];
 87                 int i = 0;
 88                 String str = "";
 89                 while ((i = fis.read(b))>0)
 90                 {
 91                     str += new String(b,0,i);
 92                 }
 93                 fis.close();
 94                 Toast.makeText(MainActivity.this, "文件内容 = "+str, Toast.LENGTH_SHORT).show();
 95             }
 96            catch (Exception e)
 97            {
 98                Toast.makeText(MainActivity.this, "读取外部文件失败", Toast.LENGTH_SHORT).show();
 99            }
100         }
101         else
102         {
103             Toast.makeText(MainActivity.this, "SD没有挂载", Toast.LENGTH_SHORT).show();
104         }
105     }
原文地址:https://www.cnblogs.com/hanazawalove/p/5536786.html