[Android实例] 调用相册和拍…

来源:互联网 发布:宁夏干部网络培训官网 编辑:程序博客网 时间:2024/05/06 19:27
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 implementsOnClickListener {
       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(BundlesavedInstanceState) {
              // TODO Auto-generated methodstub
             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 = newPopupWindow(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 methodstub
              switch (v.getId()){
              case R.id.closepop:
                    popupWindow.dismiss();
                    popupWindow.setFocusable(false);
                    break;
              caseR.id.photopage_addphoto_btn:
                    popupWindow.showAtLocation(
                                 findViewById(R.id.photopage_addphoto_btn),Gravity.BOTTOM,
                                  0, 30);
                    popupWindow.setFocusable(true);
                    popupWindow.update();
                    break;
              caseR.id.photopage_backmain_btn:
                    ImagePageActivity.this.finish();
                    break;
              caseR.id.openPhoto:
                     Intentintent = new Intent();
                    
                    intent.setType("image
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    
                    startActivityForResult(intent, 1);
                    REQUEST_CAMERA =0;
                    break;
              case R.id.camera:
                     IntentgetImageByCamera = 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(newIntent(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());  
       ContentResolvercr =this.getContentResolver();  
       try{  
          Bitmapbitmap =BitmapFactory.decodeStream(cr.openInputStream(uri));  
           
         imageView.setImageBitmap(bitmap);  
       } catch(FileNotFoundException e){  
//         Log.e("Exception",e.getMessage(),e);  
        
    }else if (REQUEST_CAMERA ==1){  
                  BitmapmyBitmap = null;
                          try  
                  
             super.onActivityResult(requestCode, resultCode,data);  
               Bundle extras= data.getExtras();  
                     myBitmap =(Bitmap)extras.get("data");  
                     ByteArrayOutputStreambaos = newByteArrayOutputStream();  
              myBitmap.compress(Bitmap.CompressFormat.JPEG, 100,baos);  
             byte[]mContent =baos.toByteArray();  
           } catch (Exception e )  
                   
            e.printStackTrace();  
                  
            //把得到的图片绑定在控件上显示  
                          imageView.setImageBitmap(myBitmap);//把拍摄的照片转成圆角显示在预览控件
    }
   super.onActivityResult(requestCode, resultCode,data);  
 

}
原创粉丝点击