[英]Android ACTION_IMAGE_CAPTURE intent crashes before returning to app
I'm trying to receive the URI of an image taken via the Mediastore.ACTION_IMAGE_CAPTURE intent, but for some reason the default camera app (Google+) crashes before it even gets back to my activity and before my code reaches onActivityResult(). 我正在尝试接收通过Mediastore.ACTION_IMAGE_CAPTURE意向拍摄的图像的URI,但由于某种原因,默认的摄像头应用(Google+)在返回我的活动之前以及在代码到达onActivityResult()之前便崩溃了。 I suspect it has something to do with the way i'm crafting the intent, but i'm not sure. 我怀疑这与我制定意图的方式有关,但我不确定。 Here's my code: 这是我的代码:
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
});
If requested I can also add my onActivityResult code, though I'm not sure it's relevant here. 如果需要,我也可以添加我的onActivityResult代码,尽管我不确定此处是否相关。
Here's the data from logcat: 这是logcat的数据:
12-02 10:47:43.976 26437-26437/? 12-02 10:47:43.976 26437-26437 /? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.google.android.apps.plus, PID: 26437 java.lang.IllegalArgumentException: A source URI must be specified via the Intent's data field. E / AndroidRuntime致命错误:主进程:com.google.android.apps.plus,PID:26437 java.lang.IllegalArgumentException:必须通过Intent的数据字段指定源URI。 at dot.a(PG:185) at com.google.android.apps.photoeditor.fragments.PlusCropActivity.onCreate(PG:93) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 在com.google.android.apps.photoeditor.fragments.PlusCropActivity.onCreate(PG:93)在dot.a(PG:185)在android.app.Activity.performCreate(Activity.java:5933)在android.app.ActivityThread.access $ 800($ 800( android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)上的ActivityThread.java:144)android.os.Looper.loop(Looper.java)上android.os.Handler.dispatchMessage(Handler.java:102)上的ActivityThread.java:144) :135),位于android.app.ActivityThread.main(ActivityThread.java:5221),位于java.lang.reflect.Method.invoke(本机方法),位于java.lang.reflect.Method.invoke(Method.java:372) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Is this because of the way im launching the intent? 这是因为我发动意图的方式吗? Is there a way to keep this intent from trying to crop the photo, as im handling cropping in a separate intent? 有没有办法避免这种意图来尝试裁剪照片,因为即时消息会以单独的意图进行处理?
Solution (I edited the code submitted by OferM): 解决方法(我编辑了OferM提交的代码):
try
{
tempFile = File.createTempFile("my_app", ".jpg");
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile("my_app", ".jpg",storageDir);
uri = Uri.fromFile(image);
}catch(Exception e){}
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtras(getIntent().getExtras());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
});
Try this: 尝试这个:
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
});
where uri
is the local path you want your image to be stored in. For example: uri
是您要在其中存储图像的本地路径。例如:
File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.