一. 前言
这章将从相册和拍照获取图片,并显示成圆形图片。
二. 准备
首先需要做一个圆形的显示区域,我用的是CircleImageView开源库
CircleImageView源码地址:https://github.com/hdodenhof/CircleImageView
(1)下载CircleImageView源码
(2)将CircleImageView.class添加到你的项目中,一般存在工具类中
(3)添加后会出现一些错误信息,这时我们需要将CircleImageView源码包中res/values/attrs.xml添加到我们的res/values下
注:attrs.xml中的参数与CircleImageView.class有关联,所有要修改就一起改。
三. 实现
activity.xml布局
xml version="1.0" encoding="utf-8"?>注:这里需要添加xmlns.app,不然会报错。手敲app后选择第二个自动导入。xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> android:id="@+id/touxiang" android:layout_width="150dp" android:layout_height="150dp" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:src="@mipmap/logo_image" app:civ_border_width="2dp" app:civ_border_color="@color/colorBlack" />
MainActivity.xml
import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.jmg.utils.CircleImageView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private CircleImageView imageView; private Bitmap head;// 头像Bitmap private static String path = "/sdcard/myHead/";// sd路径 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (CircleImageView) findViewById(R.id.touxiang); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showTypeDialog(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (resultCode == RESULT_OK) { cropPhoto(data.getData());// 裁剪图片 } break; case 2: if (resultCode == RESULT_OK) { File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg"); cropPhoto(Uri.fromFile(temp));// 裁剪图片 } break; case 3: if (data != null) { Bundle extras = data.getExtras(); head = extras.getParcelable("data"); if (head != null) { setPicToView(head);// 保存在SD卡中 imageView.setImageBitmap(head);// 用ImageButton显示出来 } } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } private void showTypeDialog() { //显示对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_select, null); TextView tv_select_gallery = (TextView) view.findViewById(R.id.tv_select_gallery); TextView tv_select_camera = (TextView) view.findViewById(R.id.tv_select_camera); tv_select_gallery.setOnClickListener(new View.OnClickListener() {// 在相册中选取 @Override public void onClick(View v) { Intent intent1 = new Intent(Intent.ACTION_PICK, null); //打开文件 intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent1, 1); dialog.dismiss(); } }); tv_select_camera.setOnClickListener(new View.OnClickListener() {// 调用照相机 @Override public void onClick(View v) { Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg"))); startActivityForResult(intent2, 2);// 采用ForResult打开 dialog.dismiss(); } }); dialog.setView(view); dialog.show(); } /** * 调用系统的裁剪功能 * * @param uri */ public void cropPhoto(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", 250); intent.putExtra("outputY", 250); intent.putExtra("return-data", true); startActivityForResult(intent, 3); } private void setPicToView(Bitmap mBitmap) { String sdStatus = Environment.getExternalStorageState(); if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用 return; } FileOutputStream b = null; File file = new File(path); file.mkdirs();// 创建文件夹 String fileName = path + "head.jpg";// 图片名字 try { b = new FileOutputStream(fileName); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { // 关闭流 if (b != null) { b.flush(); b.close(); } } catch (IOException e) { e.printStackTrace(); } } } }注:添加以上如果在android 7.0系统运行会出现奔溃
添加MyApplication.class
import android.app.Application; import android.os.StrictMode; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // android 7.0系统解决拍照的问题 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); builder.detectFileUriExposure(); } }
然后再AndroidManifest.xml中添加
android:name=".MyApplication" //添加这一行
后续还会添加各手机获取相册图片或拍照会出现的问题及解决办法。
The End!
每天进步一点点!