2015-02-24 44 views
0

我是Android應用程序開發的初學者。我已經掌握了它,並且已經閱讀了很多教程,但我仍然無法讓相機工作。基本上我想要做的就是拍攝一張照片,將圖像內部存儲在應用程序中,然後顯示該圖像(稍後),以便能夠繪製圖像。我現在不擔心繪圖部分,因爲我仍然收到SetOnClickListener的錯誤...這是代碼。試圖讓一個簡單的相機應用程序工作

import android.hardware.Camera; 
import android.net.Uri; 
import android.os.Environment; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.FrameLayout; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 


public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Create an instance of Camera 
    Camera mCamera = getCameraInstance(); 

    // Create our Preview view and set it as the content of our activity. 
    CameraPreview mPreview = new CameraPreview(this, mCamera); 
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
    preview.addView(mPreview); 

} 
private Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 

     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
     if (pictureFile == null){ 
      // Log.d("erre", "Error creating media file, check storage permissions: " + 
      //  getMessage()); 
      return; 
     } 

     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(data); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Log.d("erra", "File not found: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.d("erra", "Error accessing file: " + e.getMessage()); 
     } 
    } 
}; 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
/** A safe way to get an instance of the Camera object. */ 
public static Camera getCameraInstance(){ 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } 
    catch (Exception e){ 
     // Camera is not available (in use or does not exist) 
    } 
    return c; // returns null if camera is unavailable 
} 



// Add a listener to the Capture button 
Button captureButton = (Button) findViewById(R.id.button_capture); 

captureButton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Camera mCamera = getCameraInstance(); 
     // get an image from the camera 
     mCamera.takePicture(null, null, mPicture); 
    } 
} 
); 

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

請分享logcat或錯誤堆棧跟蹤 – LaurentY 2015-02-24 16:30:56

+0

我在哪裏使用setOnClickListener是說「<標識符預期,非法啓動的類型,)期望,期望,返回類型需要,;預計,非法啓動類型」即使我跟着完美的clickListener的語法... – nnnnnnitters 2015-02-24 16:33:27

回答

0

只要這部分移動:

// Add a listener to the Capture button 
Button captureButton = (Button) findViewById(R.id.button_capture); 

captureButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Camera mCamera = getCameraInstance(); 
     // get an image from the camera 
     mCamera.takePicture(null, null, mPicture); 
    } 
} 
); 

到您的onCreate()功能,在它的結束。那麼它應該工作。 您需要在活動的創建中找到視圖/設置點擊事件。

+0

謝謝,這固定它。但是當我點擊預覽下方的Capture按鈕時,應用程序崩潰......任何想法爲什麼? – nnnnnnitters 2015-02-24 16:59:45

+0

很難確定地知道,您需要包含日誌以幫助人們找出問題。 – 2015-02-24 17:21:32

+1

鑑於我的回答解決了您的問題,請接受它。 然後你可以嘗試自己修復這個新問題,或者在你的新問題上寫一個新帖子(確保包含logcat /崩潰日誌,否則我們將無法爲你提供幫助。 – Moonbloom 2015-02-24 18:11:11

相關問題