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();

    读取图片:

  • 相关阅读:
    【转】Delphi 关键字详解
    import datetime
    addlayer添加神经网络层
    xadmin使用富文本
    django安装xadmin
    django安装DjangoUeditor富文本
    django中admin一些方法
    Centos7.6安装python3.6.8
    django错误处理
    mysql 快速生成百万条测试数据
  • 原文地址:https://www.cnblogs.com/iamkk/p/5968161.html
Copyright © 2011-2022 走看看