2011-04-29 39 views
1

我想知道如何使用Camera API捕捉照片並保存在相機文件夾或圖庫中。 請幫我一些示例代碼。如何使用Camera API拍照?

感謝 Monali

+1

我試過但我收到異常「無法連接到相機服務。我試過camerademo例子 – Monali 2011-04-29 07:35:11

+1

你有沒有在manifest文件中設置權限? – Durga 2011-04-29 07:39:08

+0

yes我使用了 Monali 2011-04-29 08:23:34

回答

2

嘗試了這一點.. 在你的onCreate(), 字符串IMG = getImageName()添加這一點; 現在在你的onCreate()中調用下面的方法,並且應該這樣做。

private void startCamera(String ImageName) { 



    Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 


    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
      Uri.fromFile(new File(ImageName))); 

    startActivityForResult(cameraIntent, TAKE_PICTURE_WITH_CAMERA); 
} 

private String getImageName() { 
    String imgname = ""; 
    String imgpath = ""; 
    try { 
     imgname = String.format("%d.png", System.currentTimeMillis()); 

     imgpath = strDirectoy + "/" + imgname; 

     File file = new File(strDirectoy); 
     boolean exists = file.exists(); 
     if (!exists) { 
      boolean success = (new File(strDirectoy)).mkdir(); 
      if (success) 
       Log.e("Directory Created", "Directory: " + strDirectoy 
         + " created"); 
      else 

       Log.e("Directory Creation","Directory Creation failed"); 
     } 



    } catch (Exception e) { 

     e.printStackTrace(); 
    } 

    return imgpath; 
} 
+0

謝謝Andro_Selva – Monali 2011-04-29 13:13:47

1

使用mainActivity下面的代碼:

public class MainActivity extends Activity implements OnClickListener { 

     private static final int CAMERA_REQUEST= 1888; 

     private Button photoBtn; 
     private ImageView imageView; 

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

       photoBtn=(Button) findViewById(R.id.photobtn); 
       photoBtn.setOnClickListener(this); 

       imageView=(ImageView) findViewById(R.id.imageview); 
     } 

     @Override 
     public void onClick(View v) { 

       Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 

       if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){ 

        Bitmap photo=(Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo); 

       } 

     } 
    } 

- 添加以下行menifest.xml文件

  <uses-feature android:name="android.hardware.camera" /> 

- 使用activity_main.xml中的佈局是:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <Button 
         android:id="@+id/photobtn" 
         style="?android:attr/buttonStyleSmall" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentTop="true" 
         android:layout_centerHorizontal="true" 
         android:text="Open Camera" /> 

       <ImageView 
          android:id="@+id/imageview" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/photobtn" 
          android:layout_centerHorizontal="true" 
          android:src="@drawable/ic_launcher" /> 

     </RelativeLayout>