1

我在里面创建了一个撰写邮件功能,我设计了一个表面视图选项来使用移动相机录制视频,但它会强制关闭应用程序。但它适用于单独的应用程序。为什么我不能将两者集成到一个应用程序中?

这是我的代码

主要的.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_weight="1.04"             android:layout_height="wrap_content" android:layout_width="match_parent">
    <TextView android:layout_width="wrap_content" android:textAppearance="?   android:attr/textAppearanceLarge" android:id="@+id/textTo" android:layout_height="wrap_content" android:text="To : "></TextView>
    <EditText android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_below="@+id/textTo" android:inputType="textEmailAddress" android:layout_height="wrap_content" android:id="@+id/editTextTo">
        <requestFocus></requestFocus>
      </EditText>
      <TextView android:layout_alignParentLeft="true"   android:layout_width="wrap_content" android:layout_below="@+id/editTextTo" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textViewSubject" android:layout_height="wrap_content" android:text="Subject : "></TextView>
     <EditText android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_below="@+id/textViewSubject" android:layout_height="wrap_content" android:id="@+id/editTextSubject"></EditText>
     <TextView android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_below="@+id/editTextSubject" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textViewMessage" android:layout_height="wrap_content" android:text="Message : "></TextView>
    <EditText android:layout_alignParentLeft="true" android:layout_width="160dp" android:layout_below="@+id/textViewMessage" android:inputType="textMultiLine" android:layout_height="225dp" android:id="@+id/editTextMessage" android:gravity="top" android:lines="5"></EditText>
        <SurfaceView android:layout_height="225dp" android:id="@+id/videoview" android:layout_toRightOf="@+id/editTextMessage" android:layout_width="160dp" android:layout_alignBottom="@+id/editTextMessage" android:layout_alignTop="@+id/editTextMessage" android:layout_alignParentRight="false">   </SurfaceView>
      <Button android:layout_width="75dp" android:text="Send" android:layout_height="50dp" android:id="@+id/buttonSend" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/videoview" android:layout_marginLeft="18dp"></Button>
     <Button android:layout_width="75dp" android:text="Rec" android:layout_height="50dp" android:id="@+id/mybutton" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/textViewSubject"></Button>

清单文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.SampleRecord"
     android:versionCode="1"
     android:versionName="1.0">
     <uses-sdk android:minSdkVersion="8" />
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-permission android:name="android.permission.INTERNET"></uses-permission>
       <application android:icon="@drawable/icon" android:label="@string/app_name">
           <activity android:name=".SampleRecordActivity"
              android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>

   </application>
    </manifest>

SampleRecordActivity.java

     public class SampleRecordActivity extends Activity implements SurfaceHolder.Callback
  {
    Button buttonSend,btnrec;
     EditText textTo;
   EditText textSubject;
   EditText textMessage;
   Button myButton;
  MediaRecorder mediaRecorder;
   SurfaceHolder surfaceHolder;
   boolean recording;
    /** Called when the activity is first created. */
     @Override
      public void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       recording = false;

       mediaRecorder = new MediaRecorder();
       initMediaRecorder();

        SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
       surfaceHolder = myVideoView.getHolder();
       surfaceHolder.addCallback(this);
       surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       myButton = (Button)findViewById(R.id.mybutton);
       myButton.setOnClickListener(myButtonOnClickListener);
       buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextTo);
    textSubject = (EditText) findViewById(R.id.editTextSubject);
    textMessage = (EditText) findViewById(R.id.editTextMessage);


    buttonSend.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {

          String to = textTo.getText().toString();
          String subject = textSubject.getText().toString();
          String message = textMessage.getText().toString();

          Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, message);

          //need this to prompts email client only
          email.setType("message/rfc822");

          startActivity(Intent.createChooser(email, "Choose an Email client :"));

        }
    });
}
private Button.OnClickListener myButtonOnClickListener= new Button.OnClickListener()
{

   @Override
   public void onClick(View arg0) 
   {
       // TODO Auto-generated method stub
       if(recording)
       {
           mediaRecorder.stop();
           mediaRecorder.release();
           finish();
       }
       else
       {
           mediaRecorder.start();
           recording = true;
           myButton.setText("STOP");
       }
    }
};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
 // TODO Auto-generated method stub

}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
 // TODO Auto-generated method stub
 prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
 // TODO Auto-generated method stub

}

private void initMediaRecorder(){
 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
       mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
       CamcorderProfile camcorderProfile_HQ =       CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
       mediaRecorder.setProfile(camcorderProfile_HQ);
       mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
       mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
       mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
}

private void prepareMediaRecorder(){
 mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
 try {
  mediaRecorder.prepare();
 } catch (IllegalStateException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
   }
4

1 回答 1

0

尝试在 prepareMediaRecorder() 函数中添加以下行

mediaRecorder.setVideoSize(width, height)

这可能会解决运行时异常。

于 2012-10-19T08:23:32.067 回答