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!");
        }
    }
Last modification:July 12th, 2020 at 08:03 pm