Skip to content

Commit

Permalink
Fixed gallery image being overriden by cropped, Updated support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Mar 3, 2017
1 parent 833d7c7 commit ce436b7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 77 deletions.
7 changes: 0 additions & 7 deletions .idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

27 changes: 2 additions & 25 deletions README.md
Expand Up @@ -5,8 +5,6 @@ Simple image cropping tool derived from [Crop Image](https://github.com/biokys/c

Credits:
* Credits to [Alexey](https://github.com/tpom6oh) for fixing some issues related to API 23+ (Since I do not have 23+ devices yet)
* Credits to [Ashutosh Chauhan](https://github.com/ashutoshchauhan13) for adding screen orientation feature
* Credits to [Emerson Rodrigues](https://github.com/emersonrsilva) for fixing overriding of images when cropped

Features:
* Camera and/or gallery calls.
Expand Down Expand Up @@ -62,27 +60,6 @@ Sample Usage
```

Orientation List:

```
SCREEN_ORIENTATION_BEHIND
SCREEN_ORIENTATION_FULL_SENSOR
SCREEN_ORIENTATION_FULL_USER
SCREEN_ORIENTATION_LANDSCAPE
SCREEN_ORIENTATION_LOCKED
SCREEN_ORIENTATION_NOSENSOR
SCREEN_ORIENTATION_PORTRAIT
SCREEN_ORIENTATION_REVERSE_LANDSCAPE
SCREEN_ORIENTATION_REVERSE_PORTRAIT
SCREEN_ORIENTATION_SENSOR
SCREEN_ORIENTATION_SENSOR_LANDSCAPE
SCREEN_ORIENTATION_SENSOR_PORTRAIT
SCREEN_ORIENTATION_UNSPECIFIED
SCREEN_ORIENTATION_USER
SCREEN_ORIENTATION_USER_LANDSCAPE
SCREEN_ORIENTATION_USER_PORTRAIT
```

onActivityResult

```
Expand All @@ -94,13 +71,13 @@ onActivityResult
case CroperinoConfig.REQUEST_TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
/* Parameters of runCropImage = File, Activity Context, Image is Scalable or Not, Aspect Ratio X, Aspect Ratio Y, Button Bar Color, Background Color */
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0, Croperino.ScreenOrientation.SCREEN_ORIENTATION_LANDSCAPE);
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0);
}
break;
case CroperinoConfig.REQUEST_PICK_FILE:
if (resultCode == Activity.RESULT_OK) {
CroperinoFileUtil.newGalleryFile(data, MainActivity.this);
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0, Croperino.ScreenOrientation.SCREEN_ORIENTATION_LANDSCAPE);
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0);
}
break;
case CroperinoConfig.REQUEST_CROP_PHOTO:
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/mikelau/cropme/MainActivity.java
Expand Up @@ -58,13 +58,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CroperinoConfig.REQUEST_TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0, Croperino.ScreenOrientation.SCREEN_ORIENTATION_PORTRAIT);
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0);
}
break;
case CroperinoConfig.REQUEST_PICK_FILE:
if (resultCode == Activity.RESULT_OK) {
CroperinoFileUtil.newGalleryFile(data, MainActivity.this);
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0, Croperino.ScreenOrientation.SCREEN_ORIENTATION_PORTRAIT);
Croperino.runCropImage(CroperinoFileUtil.getmFileTemp(), MainActivity.this, true, 1, 1, 0, 0);
}
break;
case CroperinoConfig.REQUEST_CROP_PHOTO:
Expand Down
1 change: 1 addition & 0 deletions crop-me/src/main/AndroidManifest.xml
Expand Up @@ -13,6 +13,7 @@
android:name=".CropImage"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name"
android:screenOrientation="portrait"
/>

<provider
Expand Down
10 changes: 1 addition & 9 deletions crop-me/src/main/java/com/mikelau/croperino/CropImage.java
Expand Up @@ -20,8 +20,6 @@
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
Expand Down Expand Up @@ -93,7 +91,6 @@ public class CropImage extends MonitoredActivity {

private boolean mScaleUp = true;
private final BitmapManager.ThreadSet mDecodingThreads = new BitmapManager.ThreadSet();
private int mScreenOrientation;

@Override
public void onCreate(Bundle icicle) {
Expand Down Expand Up @@ -125,7 +122,7 @@ public void onCreate(Bundle icicle) {

mImagePath = extras.getString(IMAGE_PATH);

mSaveUri = Uri.fromFile(new File(CroperinoConfig.getsRawDirectory()+CroperinoConfig.getsImageName()));
mSaveUri = getImageUri(mImagePath);
mBitmap = getBitmap(mImagePath);

if (extras.containsKey(ASPECT_X) && extras.get(ASPECT_X) instanceof Integer) {
Expand Down Expand Up @@ -162,11 +159,6 @@ public void onCreate(Bundle icicle) {
mImageView.setBackgroundColor(extras.getInt("bgColor"));
}

mScreenOrientation = extras.getInt("orientation");
if (mScreenOrientation != -1) {
setRequestedOrientation(mScreenOrientation);
}

// Make UI fullscreen.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Expand Down
33 changes: 1 addition & 32 deletions crop-me/src/main/java/com/mikelau/croperino/Croperino.java
Expand Up @@ -20,45 +20,14 @@
*/
public class Croperino {

public enum ScreenOrientation {

SCREEN_ORIENTATION_BEHIND(3),
SCREEN_ORIENTATION_FULL_SENSOR(10),
SCREEN_ORIENTATION_FULL_USER(13),
SCREEN_ORIENTATION_LANDSCAPE(0),
SCREEN_ORIENTATION_LOCKED(14),
SCREEN_ORIENTATION_NOSENSOR(5),
SCREEN_ORIENTATION_PORTRAIT(1),
SCREEN_ORIENTATION_REVERSE_LANDSCAPE(8),
SCREEN_ORIENTATION_REVERSE_PORTRAIT(9),
SCREEN_ORIENTATION_SENSOR(4),
SCREEN_ORIENTATION_SENSOR_LANDSCAPE(6),
SCREEN_ORIENTATION_SENSOR_PORTRAIT(7),
SCREEN_ORIENTATION_UNSPECIFIED(-1),
SCREEN_ORIENTATION_USER(2),
SCREEN_ORIENTATION_USER_LANDSCAPE(11),
SCREEN_ORIENTATION_USER_PORTRAIT(12);

private final int value;

ScreenOrientation(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public static void runCropImage(File file, Activity ctx, boolean isScalable, int aspectX, int aspectY, int color, int bgColor, ScreenOrientation screenOrientation) {
public static void runCropImage(File file, Activity ctx, boolean isScalable, int aspectX, int aspectY, int color, int bgColor) {
Intent intent = new Intent(ctx, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, file.getPath());
intent.putExtra(CropImage.SCALE, isScalable);
intent.putExtra(CropImage.ASPECT_X, aspectX);
intent.putExtra(CropImage.ASPECT_Y, aspectY);
intent.putExtra("color", color);
intent.putExtra("bgColor", bgColor);
intent.putExtra("orientation", screenOrientation.getValue());
ctx.startActivityForResult(intent, CroperinoConfig.REQUEST_CROP_PHOTO);
}

Expand Down
32 changes: 30 additions & 2 deletions crop-me/src/main/java/com/mikelau/croperino/CroperinoFileUtil.java
Expand Up @@ -15,7 +15,10 @@
import com.mikelau.magictoast.MagicToast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
* Created by Mike on 9/15/2016.
Expand Down Expand Up @@ -64,15 +67,40 @@ public static File newCameraFile() throws IOException {

public static File newGalleryFile(Intent data, Context ctx) {
try {
mFileTemp = new File(CroperinoFileUtil.getPath(ctx, data.getData()));
mFileTemp = new File(CroperinoConfig.getsRawDirectory() + CroperinoConfig.getsImageName());
copyFile(new File(CroperinoFileUtil.getPath(ctx, data.getData())), mFileTemp);
return mFileTemp;
} catch (Exception e) {
MagicToast.showError(ctx, "Gallery is empty or access is prohibited by device.");
if(e instanceof IOException) {
mFileTemp = new File(CroperinoFileUtil.getPath(ctx, data.getData()));
} else {
MagicToast.showError(ctx, "Gallery is empty or access is prohibited by device.");
}
return mFileTemp;
}

}

private static void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}

FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}

public static Boolean verifyStoragePermissions(Activity activity) {
if (Build.VERSION.SDK_INT >= 23) {
int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Expand Down

0 comments on commit ce436b7

Please sign in to comment.