Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.ahmednts.vivantor.filepicker;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.ref.WeakReference;

/**
* Created by Kerollos Kromer on 17-Mar-19.
*/
public class CompressionTask extends AsyncTask<File, Integer, File> {

private static final String TAG = CompressionTask.class.getName();

private enum SupportedFormats {
JPG(".jpg"),
JPEG(".jpeg"),
PNG(".png");

private final String extension;

SupportedFormats(String extension) {
this.extension = extension;
}

@NonNull
@Override
public String toString() {
return this.extension;
}
}

/**
* file length/size in bytes.
*/
private static final long MAX_SIZE = 2 * 1024 * 1024; // 2 MB

private WeakReference<Context> contextWeakReference;
private CompressionListener compressionListener;
private boolean delete;

private File file = null;
private boolean isOriginal;

/**
* @param context context
* @param compressionListener compression process lifecycle callback
* @param delete set to true if you want to delete the new compressed file when you are done with it
*/
public CompressionTask(Context context, CompressionListener compressionListener, boolean delete) {
this.contextWeakReference = new WeakReference<>(context);
this.compressionListener = compressionListener;
this.delete = delete;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
if (compressionListener != null) compressionListener.onStart();
}

@Override
protected File doInBackground(File... files) {
Context context = contextWeakReference.get();
if (context != null) {
try {
file = compressFile(context, files[0]);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
return file;
}

@Override
protected void onPostExecute(File compressedFile) {
super.onPostExecute(compressedFile);
if (compressionListener != null && contextWeakReference.get() != null) {
if (compressedFile != null) {
compressionListener.onSuccess(compressedFile);
} else {
compressionListener.onFailure();
}
}
}

public interface CompressionListener {
void onStart();

void onSuccess(File compressedFile);

void onFailure();
}

public void dispose() {
contextWeakReference.clear();
compressionListener = null;
if (delete) deleteCompressedFile();
}

private void deleteCompressedFile() {
if (!isOriginal && file.exists() && file.isFile() && file.canWrite()) {
new Thread(() -> file.delete()).start();
}
}

/**
* we only compress if the file is bigger than {@value MAX_SIZE} , otherwise return original.
*
* @param originalFile file to be compressed {@link SupportedFormats}.
* @return original file if it is {@value MAX_SIZE} or less , otherwise compress and return the compressed file.
* @throws Exception if the file is not supported or some problem occurred during the process.
*/
@Nullable
private File compressFile(Context context, File originalFile)
throws Exception {
String fileExtension = VFileUtils.getExtension(originalFile.getPath());

if (!isValidFormat(fileExtension)) {
throw new Exception(fileExtension + " is not supported");
}

if (originalFile.length() <= MAX_SIZE) {
isOriginal = true;
return originalFile;
}

Log.d(TAG, originalFile.length() + "");

Uri fileUri = VFileUtils.getUriForFile(context, originalFile);
Bitmap b =
VFileUtils.handleSamplingAndRotationBitmap(context, fileUri);

Log.d(TAG, "Width :" + b.getWidth() + " Height :" + b.getHeight());

String fileName = "_compressed";
File compressedFile = new File(context.getCacheDir(), fileName + fileExtension);
Log.d(TAG, compressedFile.getPath());
Bitmap.CompressFormat compressFormat =
fileExtension.contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;

FileOutputStream out = new FileOutputStream(compressedFile);
b.compress(compressFormat, 100, out);
out.flush();
out.close();

Log.d(TAG, compressedFile.length() + "");
Log.d(TAG, "Width :" + b.getWidth() + " Height :" + b.getHeight());

return compressedFile;
}

private boolean isValidFormat(String fileExtension) {
SupportedFormats[] supportedFormats = SupportedFormats.values();
for (SupportedFormats supportedFormat : supportedFormats) {
if (supportedFormat.toString().equals(fileExtension)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import java.io.File;

/**
Expand Down Expand Up @@ -158,24 +157,14 @@ private void fromCameraWithFile(Activity context) {
}

if (requestPermissions(context, requestCode)) {
filePath = VFileUtils.GenerateFilePath(cameraDirectoryName, pickerType == IMAGE ? 1 : 3);
filePath = VFileUtils.generateFilePath(cameraDirectoryName, pickerType == IMAGE ? 1 : 3);

if (filePath == null) return;

//Intent intent = new Intent(action);
//intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
//context.startActivityForResult(intent, requestCode);
Intent intent = new Intent(action);
Uri fileURI;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
fileURI = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".fileprovider",
new File(filePath));
} else {
fileURI = Uri.fromFile(new File(filePath));
}
Uri fileURI = VFileUtils.getUriForFile(context, new File(filePath));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileURI);
Intent intent = new Intent(action);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileURI);
intent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (intent.resolveActivity(context.getPackageManager()) != null) {
Expand Down Expand Up @@ -284,7 +273,8 @@ public void onRequestPermissions(Activity context,
}
}

public VFileInfo onActivityResult(Activity context, int requestCode, int resultCode, Intent data) {
public VFileInfo onActivityResult(Activity context, int requestCode, int resultCode,
Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode != IMAGE_CAMERA_EXTERNAL && requestCode != VIDEO_CAMERA_EXTERNAL) {
if (data.getData() == null) return null;
Expand All @@ -298,6 +288,4 @@ public VFileInfo onActivityResult(Activity context, int requestCode, int resultC
return null;
}
}
}


}
Loading