gpt4 book ai didi

java - Android ACTION_IMAGE_CAPTURE Intent 在返回应用程序之前崩溃

转载 作者:行者123 更新时间:2023-12-01 12:11:25 24 4
gpt4 key购买 nike

我正在尝试接收通过 Mediastore.ACTION_IMAGE_CAPTURE Intent 拍摄的图像的 URI,但由于某种原因,默认相机应用程序 (Google+) 在它返回到我的 Activity 以及我的代码到达 onActivityResult() 之前就崩溃了。我怀疑这与我制定 Intent 的方式有关,但我不确定。这是我的代码:

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);
}
});

如果需要,我还可以添加我的 onActivityResult 代码,尽管我不确定它是否与此处相关。

这是来自 logcat 的数据:

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. 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)

这是因为我启动 Intent 的方式吗?有没有办法阻止这个 Intent 尝试裁剪照片,因为我在单独的 Intent 中处理裁剪?

解决方案(我编辑了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);
}
});

最佳答案

试试这个:

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);
}
});

其中 uri 是您希望存储图像的本地路径。例如:

File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);

关于java - Android ACTION_IMAGE_CAPTURE Intent 在返回应用程序之前崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27253293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com