2012-03-27 39 views
0

我使用攝像機意圖捕捉視頻。這裏是問題: 如果我使用這行代碼,我可以錄製視頻。但onActivityResult不起作用。Android攝像機:靜止圖片

Intent intent = new Intent("android.media.action.VIDEO_CAMERA"); 

如果我使用這行代碼,按下錄音按鈕後,相機被凍結,我的意思是, 畫面依然。

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 

順便說一句,當我用Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);捕獲圖片時,它工作正常。

Java文件如下:

package com.camera.picture; 
import java.io.File; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.VideoView; 
public class PictureCameraActivity extends Activity { 
    private static final int IMAGE_CAPTURE = 0; 
    private static final int VIDEO_CAPTURE = 1; 
    private Button startBtn; 
    private Button videoBtn; 
    private Uri imageUri; 
    private Uri videoUri; 
    private ImageView imageView; 
    private VideoView videoView; 

    /** Called when the activity is first created. 
    * sets the content and gets the references to 
    * the basic widgets on the screen like 
    * {@code Button} or {@link ImageView} 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     imageView = (ImageView)findViewById(R.id.img); 
     videoView = (VideoView)findViewById(R.id.videoView); 
     startBtn = (Button) findViewById(R.id.startBtn); 
     startBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       startCamera(); 
      } 
     }); 

     videoBtn = (Button) findViewById(R.id.videoBtn); 
     videoBtn.setOnClickListener(new View.OnClickListener() {    
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startVideoCamera(); 
      } 
     }); 
    } 

    public void startCamera() { 
     Log.d("ANDRO_CAMERA", "Starting camera on the phone..."); 
     String fileName = "testphoto.jpg"; 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Images.Media.TITLE, fileName); 
     values.put(MediaStore.Images.Media.DESCRIPTION, 
       "Image capture by camera"); 
     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
     imageUri = getContentResolver().insert(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
     startActivityForResult(intent, IMAGE_CAPTURE); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == IMAGE_CAPTURE) { 
      if (resultCode == RESULT_OK){ 
       Log.d("ANDROID_CAMERA","Picture taken!!!"); 
       imageView.setImageURI(imageUri); 
      } 
     } 

     if (requestCode == VIDEO_CAPTURE) { 
      if (resultCode == RESULT_OK) { 
       Log.d("ANDROID_CAMERA","Video taken!!!"); 
       Toast.makeText(this, "Video saved to:\n" + 
         data.getData(), Toast.LENGTH_LONG).show(); 
       videoView.setVideoURI(videoUri); 
      } 
     } 
    } 

    private void startVideoCamera() { 
     // TODO Auto-generated method stub 
     //create new Intent 

     Log.d("ANDRO_CAMERA", "Starting camera on the phone..."); 
     String fileName = "testvideo.mp4"; 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Video.Media.TITLE, fileName); 
     values.put(MediaStore.Video.Media.DESCRIPTION, 
       "Video captured by camera"); 
     values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4"); 
     videoUri = getContentResolver().insert(
       MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); 
     Intent intent = new Intent("android.media.action.VIDEO_CAMERA"); 
     //Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri); 
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

     // start the Video Capture Intent 
     startActivityForResult(intent, VIDEO_CAPTURE); 
    } 

    private static File getOutputMediaFile() { 
     // TODO Auto-generated method stub 
     // To be safe, you should check that the SDCard is mounted 
     // using Environment.getExternalStorageState() before doing this. 

     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
     // This location works best if you want the created images to be shared 
     // between applications and persist after your app has been uninstalled. 

     // Create the storage directory if it does not exist 
     if (! mediaStorageDir.exists()){ 
      if (! mediaStorageDir.mkdirs()){ 
       Log.d("MyCameraApp", "failed to create directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     File mediaFile; 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "VID_"+ timeStamp + ".mp4"); 
     return mediaFile; 
    } 

    /** Create a file Uri for saving an image or video */ 
    private static Uri getOutputMediaFileUri(){ 
     return Uri.fromFile(getOutputMediaFile()); 
    } 
} 

回答

0

使用MediaStore.ACTION_VIDEO_CAPTURE錄製視頻。

Intent photoPickerIntent= new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
       startActivityForResult(Intent.createChooser(photoPickerIntent,"Take Video"),TAKE_VIDEO); 

而且你OnActivityResult必須像這樣保存視頻在特定的位置:

if(requestCode==TAKE_VIDEO) 
      { 


       try 
       { 
        Log.e("videopath","videopath"); 
       AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r"); 
       FileInputStream fis = videoAsset.createInputStream(); 
       File root=new File(Environment.getExternalStorageDirectory(),"Directory"); 

        if (!root.exists()) { 
         root.mkdirs(); 
        } 
        File file; 

        file = new File(root,filename+".mp4"); 

       Uri uri=Uri.fromFile(file); 
       Item1.videopath=uri.getPath(); 

       FileOutputStream fos = new FileOutputStream(file); 

       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = fis.read(buf)) > 0) { 
        fos.write(buf, 0, len); 
       }  
       fis.close(); 
       fos.close(); 


       } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      } 
+0

謝謝你的。我是Android的新來者。該應用程序旨在記錄視頻並通過網絡傳輸到PC。你可以給我任何指導或我可以參考的一些文章。 @Abhi – Alex 2012-03-28 01:06:57

+0

這兩行代碼是用來做什麼的? 'Uri uri = Uri.fromFile(file);' 'Item1.videopath = uri.getPath();'@Abhi – Alex 2012-03-28 01:27:08

+0

它獲取存儲在SD卡中的視頻路徑... trasmit表示您想要上傳它到服務器? – Abhi 2012-03-28 05:41:55

相關問題