简体   繁体   English

Android相机意图永远不会返回到应用程序

[英]Android camera intent never returns to app

I have a very frustrating problem. 我有一个非常令人沮丧的问题。 Im using android camera intent to launch the video camera and record a video. 我使用android摄像机意图启动摄像机并录制视频。

What I expect: When I press stop I want to return to my apps activity via the onActivityResult to do some stuff like display a thumb image of the video recording etc. 我的期望:当我按Stop时,我想通过onActivityResult返回我的应用活动,以执行一些操作,例如显示录像的缩略图等。

What is happening: When i press stop the screen flashes as if returning to the app but then simply keeps on displaying the camera. 发生了什么:当我按Stop时,屏幕闪烁,就像返回到应用程序一样,但是只是继续显示相机。 I have put a log inside onActivityResult and it never calls back here! 我在onActivityResult中放了一个日志,它从不回头! Please note, this is not the issue where the intent data == null. 请注意,这不是意图数据== null的问题。 This is an issue where the method is never called at all. 这是根本不会调用该方法的问题。 When i look inside my media directory the video file is in fact there so the recording did take place and stored the file there. 当我查看我的媒体目录时,实际上是视频文件,因此确实进行了录制并将文件存储在那里。

What im doing: I am calling the camera intent from a button click inside my activity: 我在做什么:我是通过在活动中单击按钮来调用摄像机意图的:

public class MainActivity extends Activity {

  public void startVideo(){
    //set the path
    String fileName = "MyVid_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4";
    File externalFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), fileName);
    Uri videoUri = Uri.fromFile(externalFile)

    //setup up the video intent
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
    takeVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxStorage); //set the max file size - variable calculated elsewhere
    takeVideoIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,   Configuration.ORIENTATION_LANDSCAPE); //set the camera orientation
    takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, CamcorderProfile.QUALITY_480P); //set the camera resolution
    takeVideoIntent.putExtra(MediaStore.EXTRA_SHOW_ACTION_ICONS, true); //set the camera btns

    //start the intent
    if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takeVideoIntent, VIDEO_INTENT);
    }
  }

  //then i have the onActivtyResult - this part is never called, regardless of whether data is null or not
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.d("***1 LOG onActivityResult - requestCode: ", String.valueOf(requestCode));
  }

}

My permissions are as follows: 我的权限如下:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.camera" android:required="true" />

And my activity that launches the intent is declared as a portrait view in the manifest, although the camera intent is set as landscape in the code as earlier showed: 我的启动意图的活动在清单中声明为纵向视图,尽管在前面的代码中将照相机意图设置为横向:

<activity
        android:theme="@style/AppTheme"
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
</activity>

I am doing something similar for still photos which works like a charm. 我正在为静态照片做类似的事情,就像魅力一样。 This code also works fine on the emulator, but when testing on my device (Galaxy S3) it gives this behaviour and only for video. 该代码在模拟器上也可以正常工作,但是在我的设备(Galaxy S3)上进行测试时,它会显示此行为,并且仅适用于视频。 I have searched high and low for a solution but cant find anything, any advice , pointers or links would be greatly appreciated!! 我一直在寻找解决方案,但找不到任何东西,任何建议,指针或链接将不胜感激!

If you're calling it from a Fragment override the Fragment.onActivityResult() from the Fragment, otherwise you should expect it from the Activity.onActivityResult(). 如果要从Fragment调用它,请从Fragment覆盖Fragment.onActivityResult(),否则应该从Activity.onActivityResult()中获得它。 For debug purposes, add logs to both. 为了调试,将日志添加到两者。

And on the Activity one don't forget to call super.onActivityResult() otherwise it might supress the one from the Fragment. 在Activity上不要忘了调用super.onActivityResult(),否则它可能会阻止Fragment中的那个。

I think you captured the picture but didn't give an advice on what to do with it. 我认为您捕获了这张图片,但是没有给出如何处理的建议。 Please check this article at the point where you get the thumbnail and the following options. 请在获取缩略图和以下选项时查看本文。 http://developer.android.com/training/camera/photobasics.html http://developer.android.com/training/camera/photobasics.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM