调用相本和拍照得到图片

调用相册和拍照得到图片

package com.samsung.android.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;

public class ImagePageActivity extends Activity implements OnClickListener {
        private ImageButton addbtn;
        private ImageButton back_btn;
        private PopupWindow popupWindow;
        private Button openPhoto, camera, close;
        private ImageView imageView;
        private int REQUEST_CAMERA = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.photopage);
                LayoutInflater inflater = LayoutInflater.from(this);
                View layout = inflater.inflate(R.layout.popupmenu, null);
                popupWindow = new PopupWindow(layout, 200, 200);
                addbtn = (ImageButton) findViewById(R.id.photopage_addphoto_btn);
                addbtn.setOnClickListener(this);
                back_btn = (ImageButton) findViewById(R.id.photopage_backmain_btn);
                back_btn.setOnClickListener(this);
                close = (Button) layout.findViewById(R.id.closepop);
                close.setOnClickListener(this);
                camera = (Button) layout.findViewById(R.id.camera);
                camera.setOnClickListener(this);
                openPhoto = (Button) layout.findViewById(R.id.openPhoto);
                openPhoto.setOnClickListener(this);
                imageView = (ImageView)findViewById(R.id.imageView1);
        }

        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.closepop:
                        popupWindow.dismiss();
                        popupWindow.setFocusable(false);
                        break;
                case R.id.photopage_addphoto_btn:
                        popupWindow.showAtLocation(
                                        findViewById(R.id.photopage_addphoto_btn), Gravity.BOTTOM,
                                        0, 30);
                        popupWindow.setFocusable(true);
                        popupWindow.update();
                        break;
                case R.id.photopage_backmain_btn:
                        ImagePageActivity.this.finish();
                        break;
                case R.id.openPhoto:
                        Intent intent = new Intent();
                        /* 开启Pictures画面Type设定为image */
                        intent.setType("image/*");
                        /* 使用Intent.ACTION_GET_CONTENT这个Action */
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        /* 取得相片后返回本画面 */
                        startActivityForResult(intent, 1);
                        REQUEST_CAMERA =0;
                        break;
                case R.id.camera:
                        Intent getImageByCamera = new Intent(
                                        "android.media.action.IMAGE_CAPTURE");
                        startActivityForResult(getImageByCamera,REQUEST_CAMERA);
                        REQUEST_CAMERA =1;
                        break;
                default:
                        break;
                }
        }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK){
                startActivity(new Intent(ImagePageActivity.this,OrderMsgActivity.class));
                ImagePageActivity.this.finish();
        }
        return super.onKeyDown(keyCode, event);
}
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (REQUEST_CAMERA == 0) { 
        Uri uri = data.getData(); 
//        Log.e("uri", uri.toString()); 
        ContentResolver cr = this.getContentResolver(); 
        try { 
            Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); 
            /* 将Bitmap设定到ImageView */ 
            imageView.setImageBitmap(bitmap); 
        } catch (FileNotFoundException e) { 
//            Log.e("Exception", e.getMessage(),e); 
        } 
    }else if (REQUEST_CAMERA == 1){ 
                       Bitmap myBitmap = null;
                                try 
                      { 
                  super.onActivityResult(requestCode, resultCode, data); 
                    Bundle extras = data.getExtras(); 
                            myBitmap = (Bitmap) extras.get("data"); 
                            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                   myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
                 byte[] mContent = baos.toByteArray(); 
              } catch ( Exception e ) 
                        { 
                e.printStackTrace(); 
                       } 
               // 把得到的图片绑定在控件上显示 
                                imageView.setImageBitmap(myBitmap);//把拍摄的照片转成圆角显示在预览控件
    }
    super.onActivityResult(requestCode, resultCode, data); 


}