保存drawable,bitmap到本地文件夹场景
我们app开发中,需要保存一些图片,pdf,或者apk或者其他文件到本地,或者是本地存储中指定某个位置。 这个时候,就需要做2步处理 1. target sdk 大于23,现在基本上26以上,权限申请必须 2. 保存本地的操作
权限申请处理
1:申请读写权限
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2:使用简易权限申请工具,不附带其他依赖 来自郭霖大神出品的精品
GitHub:https://github.com/guolindev/PermissionX
implementation 'com.permissionx.guolindev:permissionx:1.3.0'
3:看申请代码
PermissionX.init(this)
                .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request((allGranted, grantedList, deniedList) ->
                {
                    Toast.makeText(MainActivity.this, "" + allGranted, Toast.LENGTH_SHORT).show();
		    //图片转bitmap
                    drawableToBitmap();
                });
写入本地
    private void drawableToBitmap() {
        //尝试drawable 转 bitmap
        Bitmap myLogo = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_home_base);
        if (myLogo == null) {
            Log.i(TAG, "requestPer: decodeResource fail");
            return;
        }
        //尝试bitmap转file
        boolean imageToGallery = saveImageToGallery(this, myLogo);
        if (imageToGallery) {
            Toast.makeText(this, "已保存到本地相册", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "失败了,看看log", Toast.LENGTH_LONG).show();
        }
    }
保存:
public boolean saveImageToGallery(Context context, Bitmap bmp) {
        //保存到SD卡
        final String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)
                ? Environment.getExternalStorageDirectory().getAbsolutePath()
                : "/mnt/sdcard";
        // 指定某个文件夹MyAppImage
        File appDir = new File(SAVE_PIC_PATH + "/MyAppImage/");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        long nowSystemTime = System.currentTimeMillis();
        String fileName = nowSystemTime + ".png";
        File file = new File(appDir, fileName);
        try {
            if (!file.exists()) {
                //无权限直接奔溃,或者 No such file or directory
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        //保存图片后发送广播通知更新数据库
        Uri uri = Uri.fromFile(file);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
        //        // 其次把文件插入到系统图库
        //        try {
        //            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
        //        }
        //        catch (FileNotFoundException e) {
        //            e.printStackTrace();
        //        }
        //        // 最后通知图库更新
        //        context.sendBroadcast(
        //                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
        return true;
    }
测试
- 我们app的min 19 ,target 26
- 先不申请权限,直接走保存的方法,看看效果
- 不申请权限之后,运行,看异常的log,这样就知道是不是权限问题
- 走申请权限,然后给予权限,再保存
- 进入文件管理查看MyAppImage文件夹是否存在,或者图片是否存在。
- 以上代码测试过冇问题
- ov的手机如果有的同学可以测试下



 断点-含光君  回复 @拉大锯
 断点-含光君  回复 @拉大锯 




























