Android实现拍照,相册选取、剪切图片功能
2017-08-28 本文已影响0人
pirateBay
1.代码如下
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
private static final int TAKE_PHOTO = 1000;
private static final int CHOOSE_PHOTO = 2000;
private static final int CROP_PHOTO = 3000;
private String imagePath;
private Uri imageUri;
@BindView(R.id.take_photo_button)
Button take_photo_button;
@BindView(R.id.choose_from_album_button)
Button choose_from_album_button;
@BindView(R.id.picture_imageView )
ImageView picture_imageView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
imagePath = getExternalCacheDir() + "/image.jpg"; // 设置文件保存的路径
initView();
}
private void initView() {
take_photo_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageUri = file2Uri(imagePath);
takePhoto(imageUri);
}
});
choose_from_album_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAlbum();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case TAKE_PHOTO:
// 裁剪拍摄的照片
imageUri = cropPhoto(imageUri);
break;
case CHOOSE_PHOTO:
// 裁剪从相册选择的照片
Uri uri = data.getData();
imageUri = cropPhoto(uri);
break;
case CROP_PHOTO:
// 显示图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false; // false表示返回Bitmap;true表示返回null,获取图片信息的时候用
options.inSampleSize = 2; // 设置图片宽高为原图的1/2
Bitmap bitmap = null;
try {
InputStream ins = getContentResolver().openInputStream(imageUri);
bitmap = BitmapFactory.decodeStream(ins, null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*Cursor cursor = getContentResolver().query(imageUri, null, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
}*/
// bitmap = BitmapFactory.decodeFile(imagePath, options);
picture_imageView.setImageBitmap(bitmap);
break;
default:
break;
}
}
/**
* 创建图片的File对象并转化成Uri返回
* @return
*/
private Uri file2Uri(String imagePath) {
Uri uri = null;
File file = new File(imagePath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(MainActivity.this, "cn.com.pirate.demo.fileprovier", file);
} else {
uri = Uri.fromFile(file);
}
return uri;
}
/**
* 启动相机
* @param uri
*/
private void takePhoto(Uri uri) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, TAKE_PHOTO);
}
/**
* 打开相册
*/
private void openAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
}
/**
* 裁切图片并返回Uri
* @param uri
*/
private Uri cropPhoto(Uri uri) {
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent intent = new Intent("com.android.camera.action.CROP");
// 赋予权限,否则7.0以上版本无法裁切图片
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("scale", true);
// aspectX,aspectY是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY是裁剪图片宽高
// intent.putExtra("outputX", 800);
// intent.putExtra("outputY", 800);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
return imageUri;
}
}
2.在AndroidManifest.xml中添加
<provider
android:authorities="cn.com.pirate.demo.fileprovier"
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
3.在res文件加下添加xml/file_path.xml文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name = "share" path = "" />
</paths>
4.对应的布局文件为layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.com.pirate.demo.MainActivity">
<Button
android:id="@+id/take_photo_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:textAllCaps="false"
android:text="take_photo"/>
<Button
android:id="@+id/choose_from_album_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textAllCaps="false"
android:text="choose_from_album"/>
<ImageView
android:id="@+id/picture_imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"/>
</LinearLayout>