• Android-SD卡相关操作


    SD卡相关操作

    1、获取 App 文件目录

    //获取 当前APP 文件路径
    String path1 = this.getFilesDir().getPath();
    

    当前APP目录也就是应用的这个目录 /data/data/com.tiger.helloworld/files

    d4f82ca9-8fa8-4842-bedc-978de0cdbc29.png

    2、获取外部存储器 路径

    一般手机文件管理 根路径 /storage/emulated/0/

    //获取外部存储器 路径
    String path2= Environment.getExternalStorageDirectory().getPath();
    

    3、判断SD卡是否可用

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
    

    4、获取SD总大小,可用空间

    // 获取SD卡的总大小,可用空间
    File file= Environment.getExternalStorageDirectory();
    long totalSpace= file.getTotalSpace(); //总大小
    long usableSpace= file.getUsableSpace(); //可用空间
    //转换数据格式
    String formatTotalSpace= Formatter.formatFileSize(MainActivity.this,totalSpace);
    String formatUsableSpace=Formatter.formatFileSize(MainActivity.this,usableSpace);
    
    Toast.makeText(MainActivity.this, "Total Space:"+formatTotalSpace 
            +" UsableSpace:"+formatUsableSpace, Toast.LENGTH_LONG).show();
    

    5.文本文件读写

    Type1:

    1、写入数据

         try {
             String result = userName + "_" + userPwd;
             //【1】创建一个File 类 指定我们要把数据存储的位置
             File file = new File("/data/data/com.tiger.helloworld/info.txt");
             //【2】 创建一个文件输出流
             FileOutputStream fos = new FileOutputStream(file);
             //【3】写入数据
             fos.write(result.getBytes());
             fos.close();
             return true;
         } catch (Exception e) {
             Log.e("Utils", e.getMessage());
             e.printStackTrace();
             return false;
         }
    

    2、读取数据

         try {
             File file = new File("/data/data/com.tiger.helloworld/info.txt");
             FileInputStream fis = new FileInputStream(file);
             BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
             String userInfo = bufr.readLine();
             fis.close();
             return userInfo;
         } catch (Exception e) {
             Log.e("Utils", e.getMessage());
             e.printStackTrace();
             return null;
         }
    

    Type2:

    1、通过 Content 写入数据

        try {
            String result = userName + "_" + userPwd;
            FileOutputStream fos = context.openFileOutput("info.txt", context.MODE_PRIVATE);
            fos.write(result.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            Log.e("Utils", e.getMessage());
            e.printStackTrace();
            return false;
        }
    

    2、通过 Content 读取数据

         try {
             FileInputStream fis = context.openFileInput("info.txt");
             BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
             String userInfo = bufr.readLine();
             /*while (bufr.readLine()!=null){
                 str+=bufr.readLine();
             }*/
             fis.close();
             return userInfo;
         } catch (Exception e) {
             Log.e("Utils", e.getMessage());
             e.printStackTrace();
             return null;
         }
    

    6.保存InputStream图片到本地相册中

     /**
         * 保存图片,并显示到 Gallery 中
         */
        public void saveImage(InputStream inputStream){
    
            try {
                File externalFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File directoryFile = new File(externalFile.getPath() + "/MY");
                //若目录不存在则创建
                if (!directoryFile.exists()) {
                    directoryFile.mkdirs();
                }
    
                File imageFile = new File(directoryFile, "qr"+directoryFile.list().length+".jpg");
                FileOutputStream fos=new FileOutputStream(imageFile);
    
                byte[] buffer=new byte[1024];
                int len=-1;
                while ((len=inputStream.read(buffer))!=-1){
                    fos.write(buffer,0,len);
                }
    
                inputStream.close();
                fos.close();
    
                //发送广播扫面文件,使图片显示在Gallery中
                Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(Uri.fromFile(imageFile));
                mContext.sendBroadcast(intent);
    
                Log.e("","Save successful!");
    
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("","Save unsuccessful!");
            }
        }
    
  • 相关阅读:
    linux学习(六)计划任务命令
    如何在在手机上安装linux(ubuntu )关键词:Termux
    linux学习(五)用户与组管理命令,以及用户信息文件解释
    linux学习(四)复制(cp)移动(mv)删除(rm)查找(find)文件、文件夹操作、软硬链接的区别
    Flutter中通过https post Json接收Json
    Api管家系列(一):初探
    Api管家系列(三):测试和Rest Client
    Api管家系列(二):编辑和继承Class
    JDK8 时间相关API基本使用
    windows杀端口
  • 原文地址:https://www.cnblogs.com/-Tiger/p/8762881.html
Copyright © 2020-2023  润新知