使用相机创建照片文件后,Compressor库无法找到照片文件

时间:2018-11-17 16:08:29

标签: android android-camera-intent

我正在尝试使用Compressor库压缩相机拍摄的图像,然后再将其传递到Webview。 出于某种原因,图书馆在拍照后无法找到文件。 我曾经用this library从相机上拍摄照片,并且正在使用Android Marshmallow 6.0.1在Redmi 4 Prime上运行它。

这是文件创建部分。您会看到我没有使用File.createTempFile方法,因为出于某种原因,它不能简单地在我的设备上工作,而是可以与其他设备一起工作,所以我正在生成唯一的文件名。

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp;
    File storageDir = this.activity.getApplicationContext().getExternalFilesDir(null);
    File imageFile = new File(storageDir + "/" + imageFileName + ".jpg");
    return imageFile;
}

onActivityResult的摘录

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        if(resultCode == RESULT_CANCELED){
            Utils.getFilePathCallback().onReceiveValue(null);
            Utils.setFilePathCallback(null);
            return;
        }

        if (requestCode != Utils.INPUT_FILE_REQUEST_CODE || Utils.getFilePathCallback() == null) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }

        Uri[] results = null;

        // Check that the response is a good one
        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
                // If there is not data, then we may have taken a photo
                if (Utils.getCameraPhotoPath() != null) {
                    File actualImage = new File(Utils.getCameraPhotoPath());
                    File compressedFileImage = null;
                    try {
                        compressedFileImage = new Compressor(this).setMaxWidth(640)
                                .setMaxHeight(480)
                                .setQuality(75)
                                .setCompressFormat(Bitmap.CompressFormat.WEBP)
                                .setDestinationDirectoryPath(actualImage.getParent())
                                .compressToFile(actualImage);
                    } catch (IOException e ){
                        Log.d("Compressor", e.toString());
                    }
                    //Uri compressedFileUri = Uri.fromFile(compressedFileImage);
                    //results = new Uri[]{Uri.parse(Utils.getCameraPhotoPath())};
                    results = new Uri[]{Uri.parse(compressedFileImage.getAbsolutePath())};
                }

关于此代码段。

File actualImage = new File(Utils.getCameraPhotoPath());
                    File compressedFileImage = null;
                    try {
                        compressedFileImage = new Compressor(this).setMaxWidth(640)
                                .setMaxHeight(480)
                                .setQuality(75)
                                .setCompressFormat(Bitmap.CompressFormat.WEBP)
                                .setDestinationDirectoryPath(actualImage.getParent())
                                .compressToFile(actualImage);
                    } catch (IOException e ){
                        Log.d("Compressor", e.toString());
                    }
始终定义

actualImage,图片保存在手机的“ appname / files”文件夹中。但是 compressedFileImage始终为空,因为它失败并出现 FileNotFoundException

如果我比较两条路径,它们是相等的,所以我不明白为什么它会失败。 我已经在清单文件中插入了uses-permission,并且可以在外部存储上进行读写。有点奇怪,因为this.activity.getApplicationContext().getExternalFilesDir(null);不会保存在外部存储中,而是保存在内部存储中(应用程序仍在外部存储中创建“ appname / files”文件夹)。 任何帮助表示赞赏。

0 个答案:

没有答案