版权声明:未经本人允许,转载需要注明出处 https://blog.csdn.net/nazicsdn/article/details/84315016
提供一个相机相册功能,带裁剪的,只是在类里面写了几个方法,很简单的,调用只需要一个方法,之前提供的一个工具类听不方便了,现在给大家推荐一个好用的方法,如果放到工具类里面也会很清晰明了的。
第一步就是要添加权限了:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
下面是xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="选择头像"
android:textSize="18sp" />
<ImageView
android:id="@+id/mHeader_iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:src="@mipmap/ic_launcher" />
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/mGoCamera_btn"
android:text="拍照选择"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/mGoAlbm_btn"
android:text="本地相册选择"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
下面是MainActivity 全都在这一个类里面,想必大家一看就明白了。
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mHeader_iv;
//相册请求码
private static final int ALBUM_REQUEST_CODE = 1;
//相机请求码
private static final int CAMERA_REQUEST_CODE = 2;
//剪裁请求码
private static final int CROP_REQUEST_CODE = 3;
//调用照相机返回图片文件
private File tempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mHeader_iv = (ImageView) findViewById(R.id.mHeader_iv);
Button mGoCamera_btn = (Button) findViewById(R.id.mGoCamera_btn);
Button mGoAlbm_btn = (Button) findViewById(R.id.mGoAlbm_btn);
mGoCamera_btn.setOnClickListener(this);
mGoAlbm_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.mGoCamera_btn:
getPicFromCamera();
break;
case R.id.mGoAlbm_btn:
getPicFromAlbm();
break;
default:
break;
}
}
/**
* 从相机获取图片
*/
private void getPicFromCamera() {
//用于保存调用相机拍照后所生成的文件
tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".jpg");
//跳转到调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//判断版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.hansion.chosehead", tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
Log.e("dasd", contentUri.toString());
} else { //否则使用Uri.fromFile(file)方法获取Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
}
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
/**
* 从相册获取图片
*/
private void getPicFromAlbm() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
}
/**
* 裁剪图片
*/
private void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case CAMERA_REQUEST_CODE: //调用相机后返回
if (resultCode == RESULT_OK) {
//用相机返回的照片去调用剪裁也需要对Uri进行处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.hansion.chosehead", tempFile);
cropPhoto(contentUri);
} else {
cropPhoto(Uri.fromFile(tempFile));
Log.e("返回图片路径",""+tempFile);
}
}
break;
case ALBUM_REQUEST_CODE: //调用相册后返回
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
cropPhoto(uri);
Log.e("我是相册返回",""+uri);
}
break;
case CROP_REQUEST_CODE: //调用剪裁后返回
Bundle bundle = intent.getExtras();
if (bundle != null) {
//在这里获得了剪裁后的Bitmap对象,可以用于上传
Bitmap image = bundle.getParcelable("data");
Log.e("我是裁剪后",""+image);
//设置到ImageView上
mHeader_iv.setImageBitmap(image);
//也可以进行一些保存、压缩等操作后上传
String path = saveImage("crop", image);
Log.e("我是裁剪后2",""+path);
}
break;
}
}
public String saveImage(String name, Bitmap bmp) {
File appDir = new File(Environment.getExternalStorageDirectory().getPath());
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = name + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}