我怎样才能拍到拍摄的照片?

时间:2011-10-31 07:48:40

标签: android

我在android开发中的新手。 我想开发一个简单的应用程序,可以使用手机卡马拉拍照并将其显示在手机的屏幕上。

我可以使用一些简单的例子吗?或者一些可以帮助我学习如何操作的代码?

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

开始使用相机

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

在这里你有处理

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == 0) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 

答案 1 :(得分:0)

试试这个..在onCreate中使用以下代码

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 URI mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
        "pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

    try {
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CAMERA_RESULT);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }

然后OnActivityResult

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

    super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {
           //Here you will get path of image stored in sdcard then pass it to next activity as your desires..
                           mImagePath = extras.getString("image-path");
                mSaveUri = getImageUri(mImagePath);
              Bitmap   mBitmap = getBitmap(mImagePath);
                       // here mBitmap is assigned to any imageview and you can use it in for display
         }
         }

   private Uri getImageUri(String path) {
    return Uri.fromFile(new File(path));
       }

     private Bitmap getBitmap(String path) {
     Uri uri = getImageUri(path);
     InputStream in = null;
   try {
       in = mContentResolver.openInputStream(uri);
    return BitmapFactory.decodeStream(in).copy(Config.ARGB_8888, true);
} catch (FileNotFoundException e) {
    //Log.e(TAG, "file " + path + " not found");
}
return null;
       }

            }