Andrid 调用相机拍照,图库,图片裁剪
添加读写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
首先是布局文件 activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"><de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher" /></LinearLayout>
CircleImageView
是一个加载圆形图片的控件,使用它很简单只需要导入一行依赖:
- implementation 'de.hdodenhof:circleimageview:2.1.0'
紧接着是PopopWindow : popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/pai"
android:text="拍照"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/xuan"
android:text="相册"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/qu"
android:text="取消"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
最后是 MainActivity
package com.example.dell.day02;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
// private ImageView img;
private PopupWindow popup;
private LinearLayout lin;
private Button qu;
private Button xuan;
private Button pai;
private Bitmap head;// 头像Bitmap
private static String path = "/sdcard/myHead/";
private CircleImageView img;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}private void initView() {
lin = (LinearLayout) findViewById(R.id.lin);
// img = (ImageView) findViewById(R.id.img);
img = (CircleImageView) findViewById(R.id.img);
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");// 从SD卡中找头像,转换成Bitmap
if (bt != null) {
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bt);// 转换成drawable
img.setImageDrawable(drawable);
} else {
/**
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
*
*/
}
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View inflate = getLayoutInflater().inflate(R.layout.popup, null);
popup = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
popup.setOutsideTouchable(true);
popup.showAtLocation(lin, Gravity.BOTTOM, 0, 0);
pai = inflate.findViewById(R.id.pai);
xuan = inflate.findViewById(R.id.xuan);
qu = inflate.findViewById(R.id.qu);
pai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "拍照", Toast.LENGTH_SHORT).show();
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打开
popup.dismiss();
}
});
xuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "相册选择", Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
//打开文件
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 1);
popup.dismiss();
}
});
qu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
}
});}
@Override
public 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卡中
img.setImageBitmap(head);// 用ImageButton显示出来
}
}
break;
default:
break;}
super.onActivityResult(requestCode, resultCode, data);
}/**
* 调用系统的裁剪功能
*
* @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 {
// 关闭流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Android 7.0,8.0 手机打开照相机会崩溃 请关注我的另一篇博客 里面有很详细的解决7.0 8.0相机奔溃问题