zoukankan      html  css  js  c++  java
  • Android上传头像代码,相机,相册,裁剪

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
    
        <Button
            android:id="@+id/btn_01"
            android:layout_width="150dip"
            android:layout_height="50dip"
            android:text="相册" />
    
        <Button
            android:id="@+id/btn_02"
            android:layout_width="150dip"
            android:layout_height="50dip"
            android:text="拍照" />
    
        <ImageView
            android:id="@+id/imageID"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>

    MainActivity.java

    package com.example.uploadhead;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.InputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    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.BitmapFactory;
    
    import android.view.View;
    
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
        public static final int NONE = 0;
        public static final int PHOTO_CAMERA = 1;// 相机拍照
        public static final int PHOTO_COMPILE = 2; // 编辑图片
        public static final int PHOTO_RESOULT = 3;// 结果
    
        ImageView headImageView = null;
        Button photo_button = null;
        Button camera_button = null;
        private String ImageName;
    
        public static String getStringToday() {
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateString = formatter.format(currentTime);
            return dateString;
        }
    
        
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            headImageView = (ImageView) findViewById(R.id.imageID);//找到要填充的imageView
            
            photo_button = (Button) findViewById(R.id.btn_01);// 相册按钮
            camera_button = (Button) findViewById(R.id.btn_02);// 拍照按钮
    
            
            //开启系统的相册
            photo_button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // 设置调用系统相册的意图(隐式意图)
                    Intent intent = new Intent();
                
                    //设置值活动//android.intent.action.PICK
                    intent.setAction(Intent.ACTION_PICK);
                    
                    //设置类型和数据
                    intent.setDataAndType(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            "image/*");
    
                    // 开启系统的相册
                    startActivityForResult(intent, PHOTO_COMPILE);
                }
            });
    
            
            
            
            //开启系统摄像头
            camera_button.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    //设置图片的名称
                    ImageName = "/" + getStringToday() + ".jpg";
    
                    // 设置调用系统摄像头的意图(隐式意图)
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    
                    //设置照片的输出路径和文件名
                    
                     File file=    new File(Environment.getExternalStorageDirectory(), ImageName);
                     
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                    //开启摄像头
                    startActivityForResult(intent, PHOTO_CAMERA);
                }
            });
    
        }
    
        // 调用startActivityResult,返回之后的回调函数
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == NONE)
                return;
            
            // 通过照相机拍照的图片出理
            if (requestCode == PHOTO_CAMERA) {
                // 设置文件保存路径这里放在跟目录下
                File picture = new File(Environment.getExternalStorageDirectory()
                        + ImageName);
                //裁剪图片
                startPhotoZoom(Uri.fromFile(picture));
            }
    
            if (data == null)
                return;
    
            // 读取相册裁剪图片
            if (requestCode == PHOTO_COMPILE) {
                //裁剪图片
                startPhotoZoom(data.getData());
            }
            
            
            // 裁剪照片的处理结果
            if (requestCode == PHOTO_RESOULT) {
                
                Bundle extras = data.getExtras();
            
                if (extras != null) {
                    
                    Bitmap photo = extras.getParcelable("data");
            
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 -
                                                                            // 100)压缩文件
                
                    //设置图片显示内容
                    headImageView.setImageBitmap(photo);
                }
    
            }
    
            super.onActivityResult(requestCode, resultCode, data);
        }
        
        
        /**调用系统的裁剪图片功能
         * 
         * @param uri图片的路径
         */
        public void startPhotoZoom(Uri uri) {
            
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("crop", "true");
            // aspectX aspectY 是宽高的比例
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            // outputX outputY 是裁剪图片宽高
            intent.putExtra("outputX", 64);
            intent.putExtra("outputY", 64);
            intent.putExtra("return-data", true);
            
            startActivityForResult(intent, PHOTO_RESOULT);
        }
    
    }
  • 相关阅读:
    设置数据库某字段为当前时间
    HashMap源码解析(只为吊打面试官)
    SRAM 静态内存芯片 IS62WV51216 的使用 STM32F407ZGT6
    手机ARM种类,STM32中的ARM核又是什么东东?
    运算放大器 常用经典电路 计算书
    SMT 生产线设备 (PCBA)
    横机 电控设计
    ISO26262 标准
    质量管理体系(16949)的五大工具
    IATF16949和TS16949有什么不同?
  • 原文地址:https://www.cnblogs.com/1426837364qqcom/p/5389906.html
Copyright © 2011-2022 走看看