Android 文件存储路径-内部/外部存储

2019-06-22  本文已影响0人  IT一书生

一、内存、内部存储、外部存储

概述
内存_内部存储_外部存储.png
区别
内部存储路径.png
        // 遍历外部存储路径
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            File[] files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for (File file : files) {
                Log.e("zpan", file.getAbsolutePath());
            }
        }
/storage/emulated/0/Android/data/com.example.saveimage/files/mounted 
/storage/B3E4-1711/Android/data/com.example.saveimage/files/mounted

获取外部存储的方法:

E/zpan: Environment.getExternalStorageDirectory().getAbsolutePath()
E/zpan: /storage/emulated/0
E/zpan: Environment.getExternalStoragePublicDirectory("").getAbsolutePath()
E/zpan: /storage/emulated/0
E/zpan: getExternalFilesDir("").getAbsolutePath()
E/zpan: /storage/emulated/0/Android/data/com.example.saveimage/files
E/zpan: getExternalCacheDir().getAbsolutePath()
E/zpan: /storage/emulated/0/Android/data/com.example.saveimage/cache
image.png

注意:

二、疑问

/data/user/0/com.example.saveimage/files 
/storage/emulated/0/Android/data/com.example.saveimage/files 

两者的区别是一个在内部存储里面,一个在外部存储里面。它们的共同点就是它们的路径都带有包名,表明是这个APP的专属文件,这类文件应该是随着app卸载而一起被删除的,并且我们在设置里面清除该应用的数据时,这两个文件夹下的数据都会被清除。

三、图片保存

先看段代码

    public void saveImageToAlbum(Bitmap bmp) {
        // 首先保存图片
        File cacheDir = getImageCacheDir();

        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(cacheDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "IOException", Toast.LENGTH_SHORT).show();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), fileName, null);
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(cacheDir)));
    }

    /**
     * 图片保存路径
     */
    public File getImageCacheDir() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return getExternalFilesDir("");
        }
        return createFile(Environment.getExternalStorageDirectory().getAbsolutePath(), "zpan");
    }

    public File createFile(String folderPath, String fileName) {
        File destDir = new File(folderPath, fileName);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        return destDir;
    }
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

6.0版本以上系统还要做权限的动态配置,如下:

// 申请权限
String[] mPermissionList = new String[] {
       Manifest.permission.WRITE_EXTERNAL_STORAGE, 
       Manifest.permission.READ_EXTERNAL_STORAGE
 };
ActivityCompat.requestPermissions(MainActivity.this, mPermissionList, 100);
    // 申请回执
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 100:
                boolean writeExternalStorage = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                boolean readExternalStorage = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                if (grantResults.length > 0 && writeExternalStorage && readExternalStorage) {
                    saveImageToAlbum(saveBitmap);
                } else {
                    Toast.makeText(this, "请设置存储权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

项目地址

feat-saveimage

上一篇下一篇

猜你喜欢

热点阅读