Builder for crop intents!

This also includes a helper for starting an image picker intent.
This commit is contained in:
Jamie McDonald 2014-03-12 22:33:33 +01:00
parent 3d1e3aafbb
commit ce3aa7be3c
7 changed files with 101 additions and 54 deletions

View File

@ -1,9 +1,7 @@
package com.soundcloud.android.crop.example;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
@ -12,15 +10,12 @@ import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import com.soundcloud.android.crop.CropImageActivity;
import com.soundcloud.android.crop.Crop;
import java.io.File;
public class MainActivity extends ActionBarActivity {
private static final int REQUEST_PICK_IMAGE = 10;
private static final int REQUEST_CROP_IMAGE = 11;
private Uri output;
private ImageView resultView;
@ -41,7 +36,7 @@ public class MainActivity extends ActionBarActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_select) {
pickImage();
Crop.pickImage(this);
return true;
}
return super.onOptionsItemSelected(item);
@ -50,40 +45,28 @@ public class MainActivity extends ActionBarActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case REQUEST_PICK_IMAGE:
cropImage(result.getData());
case Crop.REQUEST_PICK:
new Crop(result.getData())
.output(output)
.asSquare()
.maxX(200)
.maxY(200)
.start(this);
break;
case REQUEST_CROP_IMAGE:
if (result.getExtras().containsKey("error")) {
Exception e = (Exception) result.getSerializableExtra("error");
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} else {
resultView.setImageDrawable(null);
resultView.setImageURI(output);
}
case Crop.REQUEST_CROP:
setImage(result);
}
}
private void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
try {
startActivityForResult(intent, REQUEST_PICK_IMAGE);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.error_pick_image, Toast.LENGTH_SHORT).show();
private void setImage(Intent result) {
if (result.getExtras().containsKey("error")) {
Exception e = (Exception) result.getSerializableExtra("error");
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} else {
resultView.setImageDrawable(null);
resultView.setImageURI(output);
}
}
private void cropImage(Uri input) {
Intent intent = new Intent(this, CropImageActivity.class)
.setData(input)
.putExtra(MediaStore.EXTRA_OUTPUT, output)
.putExtra("aspectX", 1)
.putExtra("aspectY", 1)
.putExtra("maxX", 200)
.putExtra("maxY", 200);
startActivityForResult(intent, REQUEST_CROP_IMAGE);
}
}

View File

@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Crop Demo</string>
<string name="action_select">Select</string>
<string name="error_pick_image">No image providers… Whaaat?</string>
</resources>

View File

@ -1,7 +1,5 @@
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
</resources>

View File

@ -0,0 +1,75 @@
package com.soundcloud.android.crop;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;
public class Crop {
public static final int REQUEST_CROP = 10;
public static final int REQUEST_PICK = 11;
static interface Extra {
String ASPECT_X = "aspect_x";
String ASPECT_Y = "aspect_y";
String MAX_X = "max_x";
String MAX_Y = "max_y";
String ERROR = "error";
}
private Intent cropIntent;
public Crop(Uri input) {
cropIntent = new Intent();
cropIntent.setData(input);
}
public Crop output(Uri output) {
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);
return this;
}
public Crop aspectX(int x) {
cropIntent.putExtra(Extra.ASPECT_X, x);
return this;
}
public Crop aspectY(int y) {
cropIntent.putExtra(Extra.ASPECT_Y, y);
return this;
}
public Crop maxX(int x) {
cropIntent.putExtra(Extra.MAX_X, x);
return this;
}
public Crop maxY(int y) {
cropIntent.putExtra(Extra.MAX_Y, y);
return this;
}
public Crop asSquare() {
cropIntent.putExtra(Extra.ASPECT_X, 1);
cropIntent.putExtra(Extra.ASPECT_Y, 1);
return this;
}
public void start(Activity activity) {
cropIntent.setClass(activity, CropImageActivity.class);
activity.startActivityForResult(cropIntent, REQUEST_CROP);
}
public static void pickImage(Activity activity) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
try {
activity.startActivityForResult(intent, REQUEST_PICK);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.error_pick_image, Toast.LENGTH_SHORT).show();
}
}
}

View File

@ -50,15 +50,8 @@ import java.util.concurrent.CountDownLatch;
public class CropImageActivity extends MonitoredActivity {
private static final String TAG = CropImageActivity.class.getSimpleName();
public static final boolean IN_MEMORY_CROP = Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD_MR1;
public static interface Extras {
String ASPECT_X = "aspectX";
String ASPECT_Y = "aspectY";
String MAX_X = "maxX";
String MAX_Y = "maxY";
String ERROR = "error";
}
public static final boolean IN_MEMORY_CROP = Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD_MR1;
private int mAspectX, mAspectY;
private final Handler mHandler = new Handler();
@ -97,10 +90,10 @@ public class CropImageActivity extends MonitoredActivity {
Bundle extras = intent.getExtras();
if (extras != null) {
mAspectX = extras.getInt(Extras.ASPECT_X);
mAspectY = extras.getInt(Extras.ASPECT_Y);
mMaxX = extras.getInt(Extras.MAX_X);
mMaxY = extras.getInt(Extras.MAX_Y);
mAspectX = extras.getInt(Crop.Extra.ASPECT_X);
mAspectY = extras.getInt(Crop.Extra.ASPECT_Y);
mMaxX = extras.getInt(Crop.Extra.MAX_X);
mMaxY = extras.getInt(Crop.Extra.MAX_Y);
mSaveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
}
@ -460,7 +453,7 @@ public class CropImageActivity extends MonitoredActivity {
}
private void setResultByUriWithException(Uri uri, Exception exception){
setResult(RESULT_OK, new Intent().putExtra(MediaStore.EXTRA_OUTPUT, uri).putExtra(Extras.ERROR, exception));
setResult(RESULT_OK, new Intent().putExtra(MediaStore.EXTRA_OUTPUT, uri).putExtra(Crop.Extra.ERROR, exception));
}
public static int getExifRotation(File imageFile) {

View File

@ -75,7 +75,6 @@ public class Util {
}
}
@Override
public void onActivityDestroyed(MonitoredActivity activity) {
// We get here only when the onDestroyed being called before

View File

@ -2,6 +2,7 @@
<string name="saving">Saving picture…</string>
<string name="wait">Please wait…</string>
<string name="error_pick_image">No image sources available</string>
<string name="done">DONE</string>
<string name="cancel">CANCEL</string>