2013-06-25 317 views
0

我拍照和保存的代碼有問題。它在我launchCamera()時崩潰。 你能幫我嗎?從相機拍攝照片

private void launchCamera() { 
    try { 

     mOutputFile = File.createTempFile("prova", null); 

      Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

      intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, 
       Uri.fromFile(mOutputFile)); 

      startActivityForResult(intentCamera, CAMERA_REQUEST); 
    } catch (Exception e) { 
     Toast t = Toast.makeText(this, "ERROR:\n" + e.toString(), Toast.LENGTH_LONG); 
     t.show(); 
    } 
} 
+0

什麼是logcat的說? –

+0

該程序啓動異常,我不知道爲什麼 – Juppy

+2

@Juppy這是我見過的最有趣的事情logcat說。儘管如此,請複製LogCat文本並將其粘貼到此處。 – BLaZuRE

回答

0

也許你沒有添加清單文件所需的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 

此外,我建議你去了這個博客帖子我就用拍照這個話題寫內置的攝像頭活動:

Take Picture with build in Camera Activity

0

要訪問設備攝像頭,您必須聲明攝像頭權限在你的Android清單

+0

我宣佈相機權限尚未 – Juppy

1

我使用這段代碼嘗試一下:

/** 
* This method is used to start the camera activity and save the image taken as the imagename passed 
* 
* @param imagename : this is the name of the image which will be saved 
*/ 
private void clickPicture(String imagename) { 
    Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE"); 
    File cameraFolder; 
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
     cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"myfolder/"); 
    else 
     cameraFolder= context.getCacheDir(); 
    if(!cameraFolder.exists()) 
     cameraFolder.mkdirs(); 
    String imageFileName = imagename; 
    File photo = new File(Environment.getExternalStorageDirectory(), "myfolder/" + imageFileName); 
    getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
    Uri.fromFile(photo); 
    startActivityForResult(getCameraImage, 1); 

} 

,並在您的清單文件中添加在未經許可:

<uses-permission android:name="android.permission.CAMERA" ></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

爲您代碼沒關係? – Juppy

相關問題