gpt4 book ai didi

android - 在 MediaStore.INTENT_ACTION_VIDEO_CAMERA 的情况下没有 startActivityForResult 的回调

转载 作者:行者123 更新时间:2023-11-30 04:59:15 25 4
gpt4 key购买 nike

案例是当用户选择相机时,他可以灵活地捕捉图像或录制视频,并且用户应该能够在自己的应用程序中显示相同的内容。

对于这种情况,使用了 MediaStore.INTENT_ACTION_VIDEO_CAMERA Intent ,但在捕获图像或录制视频的情况下都没有回调。

Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivityForResult(intent, VIDEO_CAMERA);

为了得到结果,代码使用如下:-

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}

if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri = data.getData();
}
}
}

最佳答案

我终于找到了解决方案,如下:-

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent,VIDEO_CAMERA);

最后在 onActivityResult 中收到回调,我以这种方式获得了 uri:-

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}

if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri;
if (data == null || data.getData() == null) {
Bitmap bitmap (Bitmap)data.getExtras().get("data");
// TODO:Get uri from bitmap for image
uri = getImageUri(context, bitmap);
} else {
//Get uri for video
uri = data.getData();
}


}
}
}

关于android - 在 MediaStore.INTENT_ACTION_VIDEO_CAMERA 的情况下没有 startActivityForResult 的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523118/

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