2010-07-12 86 views
1

我試圖將視頻記錄到外部SD卡。但是,每次我嘗試並記錄數據時 - 我總是得到一個java.io.FileNotFound異常。我想知道是否有人知道任何教程或可以幫助糾正我的代碼。將視頻記錄到Android上的外部SD卡

這裏是它試圖錄制視頻

public class VideoActivity extends Activity { 

private SurfaceView preview; 
private SurfaceHolder previewHolder; 
private String locationName; 
private String filepath; 
private File video; 

public void onCreate(Bundle videocawk) { 
    super.onCreate(videocawk); 
    setContentView(R.layout.video_layout); 
    setSurface(); 
    locationName = getIntent().getStringExtra("locationName"); 
    filepath = getFilePath(locationName); 
    try { 
     MediaRecorder r = getMediaRecorder(filepath, previewHolder 
       .getSurface()); 
     setSurfaceCallback(preview,r); 
     setButtonListeners(r); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

private String getFilePath(String locName) { 
    String dir = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    String add = "/test/data/video/"; 
    String name = locName + "--1"; 
    String total = dir + add + name; 
    video = new File(total); 
    return total; 
} 

private void setSurface() { 
    preview = (SurfaceView) findViewById(R.id.preview); 
    previewHolder = preview.getHolder(); 
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

private void setButtonListeners(final MediaRecorder r) { 
    Button start = (Button) findViewById(R.id.start_video); 
    Button end = (Button) findViewById(R.id.stop_video); 

    start.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      startRecording(r); 

     } 
    }); 

    end.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      stopRecording(r); 
      setPassPrefs(); 
      startActivity(setPassPrefs()); 
      finish(); 

     } 
    }); 

} 

private void setSurfaceCallback(SurfaceView s, final MediaRecorder r) 
{ 


SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() { 
    public void surfaceCreated(SurfaceHolder holder) { 

     try { 
      r.setPreviewDisplay(previewHolder.getSurface()); 
     } catch (Throwable t) { 
      Log.e("PictureDemo-surfaceCallback", 
        "Exception in setPreviewDisplay()", t); 
      Toast.makeText(VideoActivity.this, t.getMessage(), 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 

    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     r.stop(); 
     r.release(); 

    } 
}; 

previewHolder.addCallback(surfaceCallback); 
} 

private Intent setPassPrefs() { 
    AttachedImageAdapter adapter = new AttachedImageAdapter(locationName, 
      VideoActivity.this); 
    adapter.setVideoPath(filepath); 
    Intent i = new Intent(VideoActivity.this, EnterTag.class); 
    i.putExtras(getIntent()); 
    return i; 

} 

private void startRecording(MediaRecorder r) { 
    r.start(); 
} 

private void stopRecording(MediaRecorder r) { 
    r.stop(); 
} 

private MediaRecorder getMediaRecorder(String filepath, Surface s) 
     throws IllegalStateException, IOException { 
    MediaRecorder m_recorder = new MediaRecorder(); 
    m_recorder.setPreviewDisplay(s); 
    m_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
    m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
    m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
    m_recorder.setMaxDuration(20000); // length of video in MS 
    m_recorder.setVideoSize(320, 240); 
    m_recorder.setVideoFrameRate(15); 
    m_recorder.setOutputFile(video.getPath()); 
    m_recorder.prepare(); 

    return m_recorder; 
} 

}

任何幫助,將不勝感激類 - 提前再次感謝。此外,這裏有一個pastebin,它具有視頻活動引用的佈局。

Here是我的錯誤日誌的PasteBin - 如果有幫助。

+0

順便說一句 - 我在我的清單中聲明瞭WRITE_EXTERNAL_STORAGE權限。 – hwrdprkns 2010-07-12 03:51:09

+0

您確定額外的'locationname'實際設置了嗎?如果你自己啓動這個應用程序,它可能不會。嘗試將其更改爲文字以進行測試,然後查看它是否有效。 – 2010-07-12 04:51:43

+0

實際上,我確定它的設置是因爲在「FileNotFound」錯誤中,它正確地輸出了位置名稱 我將在一秒鐘內粘貼我的LogCat – hwrdprkns 2010-07-12 05:08:39

回答

2

我能想到的兩件事:

1:您的SD卡可能未安裝。

if (android.os.Environment.getExternalStorageState() != android.os.Environment.MEDIA_MOUNTED) then you have a problem. 

2:文件路徑中的所有目錄可能不存在。

filepath = getFilePath(locationName); 
File file = new File(filepath); 
File parentDir = file.getParentFile(); 
if (!parentDir.exists()) 
{ 
parentDir.mkdirs(); 
} 
+0

感謝您的信息 - 我會盡力回覆 – hwrdprkns 2010-07-12 19:59:12

+0

工作得很好 - 謝謝。 – hwrdprkns 2010-07-13 03:15:14