I'm trying to compress an image taken with the camera, right before passing it to a Webview
, with the Compressor library.
For some reason, the library can't find the file right after taking the picture.
I've used this library to take pictures from camera, and I'm running it on a Redmi 4 Prime with Android Marshmallow 6.0.1.
This is the file creation Part. You can see I'm not using a File.createTempFile
method because for some reason it wouldn't simply work on my device but work with others, so I'm generating a unique filename instead.
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;
}
and a snippet from 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())};
}
About this snippet.
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
is always defined, the picture is saved on the phone in the "appname/files" folder. But compressedFileImage
is always null, as it fails with an FileNotFoundException.
If I compare the two paths, they're equal, so I don't understand why it should fail.
I've already inserted uses-permission
in the manifest file, and I can write and read on external storage. A bit weird since this.activity.getApplicationContext().getExternalFilesDir(null);
doesn't save in the external storage but in the internal one instead (the app still creates a "appname/files" folder in the external).
Any help is appreciated.