2013-12-23 55 views
1

我用下面的代碼來調用已有相機:警告:相機失敗

// New intent to Camera feature 
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
Uri fileUri = Uri.fromFile((new File((new Date()).toString()))); // create a file to save the video 

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high 

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

可以拍攝照片,但不能錄製視頻,我收到了錯誤Warning : Camera failed

我試着做一些與some solutions有關的事情,但不能收到好的結果。 (雖然重置手機)

請告訴我如何解決這個問題,

感謝,

P/S:設備 - 三星Galaxy Tab 7 2.2.1

編輯: 我用下面的代碼來接收響應,並響應結果resultCode == RESULT_CANCELED

if (resultCode == RESULT_OK) { 
    // Video captured and saved to fileUri specified in the Intent 
    Toast.makeText(this, "Video saved to:\n" + 
        data.getData(), Toast.LENGTH_SHORT).show(); 
} else if (resultCode == RESULT_CANCELED) { 
    // User cancelled the video capture 
    Toast.makeText(this, "User cancelled the video capture", Toast.LENGTH_SHORT).show(); 
} else { 
    // Video capture failed, advise user 
    Toast.makeText(this, "Warning : Camera failed", Toast.LENGTH_SHORT).show(); 
} 
+0

爲什麼你不檢查是否實際返回意圖?類似於這個http://developer.android.com/training/camera/videobasics.html也檢查這個線程http://stackoverflow.com/questions/2550743/android-video-capture-sample-app –

+0

@Boris:我編輯代碼請按照您的評論。我也運行第二個鏈接(項目),我得到黑屏@@什麼也沒有發生。 –

+0

你必須創建新的活動添加Surfaceholder和MediaRecorder類的句柄videorecording很容易 – Sanket990

回答

0

我比較了Android Guide你的代碼。唯一的區別是文件uri。你可以試試谷歌的示例代碼來獲取文件uri:

public static final int MEDIA_TYPE_IMAGE = 1; 
public static final int MEDIA_TYPE_VIDEO = 2; 

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

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type){ 
    // 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; 
    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
     "IMG_"+ timeStamp + ".jpg"); 
    } else if(type == MEDIA_TYPE_VIDEO) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
     "VID_"+ timeStamp + ".mp4"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 
+0

如果我的設備還沒有安裝SdCard還有什麼問題,會出現什麼問題?我仍然使用這個功能好嗎? –

+0

至少你有間隔存儲權嗎?該方法意味着您可以獲取設備的公共目錄。某些設備可能只有內部存儲器,並非所有設備都有像三星設備那樣的額外sdcard插槽。所以這種方法可以很好地工作 – yushulx

+0

是的,至少我有內部存儲。所以你的回答與我的錯誤沒有關係。抱歉。 –