• android 调取相册和拍照 图片 剪切获取图片


    package com.xiaogang.imageheader;  
      
    import java.io.File;  
      
    import android.net.Uri;  
    import android.os.Bundle;  
    import android.os.Environment;  
    import android.provider.MediaStore;  
    import android.app.Activity;  
    import android.content.Intent;  
    import android.graphics.Bitmap;  
    import android.graphics.drawable.BitmapDrawable;  
    import android.graphics.drawable.Drawable;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.ImageView;  
    import android.widget.Toast;  
      
    public class MainActivity extends Activity implements OnClickListener {  
        private static final int IMAGE_REQUEST_CODE = 0;  
        private static final int CAMERA_REQUEST_CODE = 1;  
        private static final int RESIZE_REQUEST_CODE = 2;  
      
        private static final String IMAGE_FILE_NAME = "header.jpg";  
      
        private ImageView mImageHeader;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
      
            setupViews();  
        }  
      
        private void setupViews() {  
            mImageHeader = (ImageView) findViewById(R.id.image_header);  
            final Button selectBtn1 = (Button) findViewById(R.id.btn_selectimage);  
            final Button selectBtn2 = (Button) findViewById(R.id.btn_takephoto);  
            selectBtn1.setOnClickListener(this);  
            selectBtn2.setOnClickListener(this);  
        }  
      
        @Override  
        public void onClick(View v) {  
            switch (v.getId()) {  
            case R.id.btn_selectimage:  
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);  
                galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);  
                galleryIntent.setType("image/*");  
                startActivityForResult(galleryIntent, IMAGE_REQUEST_CODE);  
                break;  
            case R.id.btn_takephoto:  
                if (isSdcardExisting()) {  
                    Intent cameraIntent = new Intent(  
                            "android.media.action.IMAGE_CAPTURE");  
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());  
                    cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);  
                    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);  
                } else {  
                    Toast.makeText(v.getContext(), "请插入sd卡", Toast.LENGTH_LONG)  
                            .show();  
                }  
                break;  
            }  
        }  
      
        @Override  
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (resultCode != RESULT_OK) {  
                return;  
            } else {  
                switch (requestCode) {  
                case IMAGE_REQUEST_CODE:  
                    resizeImage(data.getData());  
                    break;  
                case CAMERA_REQUEST_CODE:  
                    if (isSdcardExisting()) {  
                        resizeImage(getImageUri());  
                    } else {  
                        Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!",  
                                Toast.LENGTH_LONG).show();  
                    }  
                    break;  
      
                case RESIZE_REQUEST_CODE:  
                    if (data != null) {  
                        showResizeImage(data);  
                    }  
                    break;  
                }  
            }  
      
            super.onActivityResult(requestCode, resultCode, data);  
        }  
      
        private boolean isSdcardExisting() {  
            final String state = Environment.getExternalStorageState();  
            if (state.equals(Environment.MEDIA_MOUNTED)) {  
                return true;  
            } else {  
                return false;  
            }  
        }  
      
        public void resizeImage(Uri uri) {  
            Intent intent = new Intent("com.android.camera.action.CROP");  
            intent.setDataAndType(uri, "image/*");  
            intent.putExtra("crop", "true");  
            intent.putExtra("aspectX", 1);  
            intent.putExtra("aspectY", 1);  
            intent.putExtra("outputX", 150);  
            intent.putExtra("outputY", 150);  
            intent.putExtra("return-data", true);  
            startActivityForResult(intent, RESIZE_REQUEST_CODE);  
        }  
      
        private void showResizeImage(Intent data) {  
            Bundle extras = data.getExtras();  
            if (extras != null) {  
                Bitmap photo = extras.getParcelable("data");  
                Drawable drawable = new BitmapDrawable(photo);  
                mImageHeader.setImageDrawable(drawable);  
            }  
        }  
      
        private Uri getImageUri() {  
            return Uri.fromFile(new File(Environment.getExternalStorageDirectory(),  
                    IMAGE_FILE_NAME));  
        }  
    }  
  • 相关阅读:
    (转)二步实现 远程连接 阿里云SqlServer 2012 数据库服务器
    浅谈C#在网络波动时防重复提交
    面向 Kubernetes 编程: Kubernetes 是下一代操作系统
    C#并行编程(2):.NET线程池
    C#并行编程(1):理解并行
    iOS 动画总结----UIView动画
    iOS开发之各种动画各种页面切面效果
    iOS动画效果和实现
    iOS开发动画(Animation)总结
    iOS开发之传感器
  • 原文地址:https://www.cnblogs.com/xiaoli3007/p/4220858.html
Copyright © 2020-2023  润新知