lexluthors/CompressTools-Android

创建文件失败

Opened this issue · 4 comments

 CompressTools.newBuilder(mView.getContext())
                .setKeepResolution(true)
                .setBitmapFormat(Bitmap.CompressFormat.PNG)
                .setDestinationDirectoryPath(FileManager.getInstance().getPicDirPath())
                .setQuality(70)
                .build()
                .compressToFile(file, new CompressTools.OnCompressListener() {
                    @Override
                    public void onStart() {
                        Log.d("TAG", "onStart: ");
                    }

                    @Override
                    public void onFail(String error) {
                        Log.e(TAG, "onFail: " + error);
                    }

                    @Override
                    public void onSuccess(File file) {
                        LogUtil.d("压缩后" + file.length() / 1024);
                    }
                });

其中 FileManager.getInstance().getPicDirPath() 经log为/storage/emulated/0/Android/data/com.xxx.xxx/cache/pic
应无权限问题,已在该目录下成功写入多个文件

@Van1996 报什么错?是不是没有配置动态权限?

报错log为: E/GifHeaderParser: onFail: 创建文件失败
有配置动态权限:Manifest.permission.WRITE_EXTERNAL_STORAGE

@Van1996 使用默认路径试试,就是demo中的,你看看demo里面的。如果可以,说明你传的路径有问题

改用demo的默认路径:

FileUtil.getPhotoFileDir().getAbsolutePath()

以后的代码(如下),依旧提示“ 创建文件失败”

        Bitmap afterCut = cutPicture(bitmap);
        String afterCutPath = FileManager.getInstance().getEmptyPng(1);
        BitmapUtil.writeIntoFile(afterCut, afterCutPath);
        // 将裁剪完的bitmap保存到本地

        File file = new File(afterCutPath);
        LogUtil.d("仅裁剪" + file.length() / 1024);

        CompressTools.newBuilder(mView.getContext())
                .setKeepResolution(true)
                .setBitmapFormat(Bitmap.CompressFormat.PNG)
                .setDestinationDirectoryPath(FileUtil.getPhotoFileDir().getAbsolutePath())
                .setQuality(70)
                .build()
                .compressToFile(file, new CompressTools.OnCompressListener() {
                    @Override
                    public void onStart() {
                        Log.d("TAG", "onStart: ");
                    }

                    @Override
                    public void onFail(String error) {
                        LogUtil.d(FileUtil.getPhotoFileDir().getAbsolutePath());
//                        Log.d(TAG, FileManager.getInstance().getPicDirPath());
                        Log.e(TAG, "onFail: " + error);
                    }

                    @Override
                    public void onSuccess(File file) {
                        LogUtil.d("压缩后" + file.length() / 1024);
                        mView.onBitmapHandleFinished(file.getAbsolutePath(), file.getAbsolutePath());
                    }
                });

log为:

D/TAG: onStart:
D/Log.d_default_tag: /storage/emulated/0/DCIM/Cherry

重新审视代码,发现在压缩代码片段上面,还有一段代码,该代码有文件写入的操作(将bitmap写入文件)
并且检查得知,文件写入是没有问题,说明应该没有权限和路径的问题