zoukankan      html  css  js  c++  java
  • android-数据存储之手机内部file存储

    一、基础概要

      1、说明:

        1>应用程序运行需要一些较大的数据或者图片可保存在手机内部

        2>文件类型:任意

        3>路径:/data/data/packageName/files/

        4>卸载应用时会删除此数据文件

        5>也可以设置操作数据文件的权限(同SharedPreferences)

    二、练习

      1>FileInputStream fis=openFileInput("logo.png");     读取文件

      2>FileOutputStream fos=openFileOutput("logo.png",MODE_PRIVATE);   保存文件

      3>File filesDir=getFilesDir();    得到files文件夹对象

      4>操作asserts下的文件:

          .context.getAssets()    得到AssetManager

          .InputStream open(filename);  读取文件

      5>加载图片文件:

        Bitmap bitmap=BitmapFactory.decodeFile(String pathName);  (Drawable:表示可绘制图片对象)

      

    保存图文件:

      1>得到InputStream  :读取assets下的logo.png

          AssetManager manager=getAssets();

      2>读取文件

          InputStream is=manager.open("logo.png");

      3>得到OutputStream  : /data/data/packageName/files/logo.png

          FileOutputStream fos=openFileOutput("logo.png",Context.MODE_PRIVATE);

      4>边读边写:

        byte[] buffer=new byte[1024];

        int len=-1;

        while((len=is.read(buffer))!=-1){

          fos.write(buffer,0,len);

        }

        fos.close();

        is.close();

    读取图片:

      1>得到图片路径:  /data/data/packageName/files

        String filesPath=getFileDir().getAbsolutePath();

        String imgPath=filesPath+"/logo.png";

      2>加载图片文件得到bitmap对象:

        Bitmap bitmap=BitmapFactory.decodeFile(imgPath);

      3>将其设置到imageView中显示:

        iv_if.setImageBitmap(bitmap);

    三、源代码

    保存图片:

    AssetManager manager=getAssets();
         InputStream is=manager.open("logo.png");
         FileOutputStream fos= openFileOutput("logo.png",Context.MODE_PRIVATE);
         byte[] buffer=new byte[1024];
         int len=-1;
         while((len=is.read(buffer))!=-1){
          fos.write(buffer, 0, len);
         }
         fos.close();
         is.close();
         Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

    读取图片:

  • 相关阅读:
    简单说说Restful API
    事务的ACID特性
    WPF DataGrid与ListView性能对比与场景选择
    WPF MVVM实现INotifyPropertyChanged数据监听
    学习Promise异步编程
    CSS单位
    Mac Docker设置国内镜像加速器
    Mac下安装Redis,附可视化工具Medis
    预告 | XPocket开源产品发布会将于1月15日正式线上直播!
    CSDN资源免费下载
  • 原文地址:https://www.cnblogs.com/iamkk/p/5968161.html
Copyright © 2011-2022 走看看