Tuesday, February 21, 2012

[android-developers] How to take photo and crop with Android 3.x

Hi,

I've trying to develop an activity on Android 3.x with a button that a
user will be able to take a photo and right after that to crop this
photo.
The photo is taken fine and saved but when I send the intent to crop
it I get the following excpetion:

a02-21 10:58:36.460: E/AndroidRuntime(14379): FATAL EXCEPTION: main
02-21 10:58:36.460: E/AndroidRuntime(14379):
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=100, result=-1, data=null} to activity
{com.tmp.cameratest/com.tmp.cameratest.CameraTestActivity}:
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=com.android.camera.action.CROP dat=file:///mnt/sdcard/
DCIM/MyCameraApp/IMG_20120221_105828.jpg (has extras) }
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.ActivityThread.deliverResults(ActivityThread.java:2726)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.ActivityThread.handleSendResult(ActivityThread.java:2769)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.ActivityThread.access$2000(ActivityThread.java:123)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1023)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.os.Handler.dispatchMessage(Handler.java:99)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.os.Looper.loop(Looper.java:126)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.ActivityThread.main(ActivityThread.java:3997)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
java.lang.reflect.Method.invokeNative(Native Method)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
java.lang.reflect.Method.invoke(Method.java:491)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
dalvik.system.NativeStart.main(Native Method)
02-21 10:58:36.460: E/AndroidRuntime(14379): Caused by:
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=com.android.camera.action.CROP dat=file:///mnt/sdcard/
DCIM/MyCameraApp/IMG_20120221_105828.jpg (has extras) }
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:
1508)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.Instrumentation.execStartActivity(Instrumentation.java:
1382)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.Activity.startActivityForResult(Activity.java:3038)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
com.tmp.cameratest.CameraTestActivity.onActivityResult(CameraTestActivity.java:
64)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.Activity.dispatchActivityResult(Activity.java:4477)
02-21 10:58:36.460: E/AndroidRuntime(14379): at
android.app.ActivityThread.deliverResults(ActivityThread.java:2722)
02-21 10:58:36.460: E/AndroidRuntime(14379): ... 11 more

Here is my code:

public class CameraTestActivity extends Activity {

private static final int CAMERA_REQUEST = 100;
private static final int CROP_REQUEST = 200;

private File mFile;

private static final String LOG = "Camera-Test";

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {

if (mFile.isFile() && mFile.exists()) {
Log.d(LOG, "File exist");
}

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
intent.setData(Uri.fromFile(mFile));

intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", "true");
intent.putExtra("return-data", true);

startActivityForResult(intent, CROP_REQUEST);

} else if (resultCode == RESULT_CANCELED) {

}
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button camera = (Button)findViewById(R.id.buttonCamera);
camera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
getOutputMediaFileUri(MEDIA_TYPE_IMAGE));

startActivityForResult(intent, CAMERA_REQUEST);
}
});
}

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri for saving an image or video */
private Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private File getOutputMediaFile(int type){

Log.d("Camera-Test",
Environment.getExternalStorageDirectory().toString());

File mediaStorageDir = new
File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "MyCameraApp");

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}

// Create a media file name
String timeStamp = new
SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() +
File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() +
File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}

mFile = mediaFile;

return mediaFile;
}
}


Does anybody know why do I get this exception and it doesn't work?

Thank you

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment