隨筆
最近在做的一個項(xiàng)目中有用到從圖庫中選擇圖片她倘,然后進(jìn)行裁剪后再使用的功能脯厨,在這里也簡單的記錄下整個實(shí)現(xiàn)的過程铅祸,以備后查。
首先來看看布局文件合武,非常的簡單临梗,兩個按鈕,一個ImageView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/take_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="拍照"
android:textSize="16sp"/>
<Button
android:id="@+id/select_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="選擇圖片"
android:textSize="16sp"/>
<ImageView
android:id="@+id/photo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>
</LinearLayout>
然后再來看看代碼部分
package com.example.goblit.selectpic;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class SelectPicActivity extends Activity implements View.OnClickListener {
private Button take_photo_btn;
private Button select_photo_btn;
private ImageView photo_iv;
//使用照相機(jī)拍照獲取圖片
public static final int TAKE_PHOTO_CODE = 1;
//使用相冊中的圖片
public static final int SELECT_PIC_CODE = 2;
//圖片裁剪
private static final int PHOTO_CROP_CODE = 3;
//定義圖片的Uri
private Uri photoUri;
//圖片文件路徑
private String picPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
this.take_photo_btn = (Button) findViewById(R.id.take_photo_btn);
this.take_photo_btn.setOnClickListener(this);
this.select_photo_btn = (Button) findViewById(R.id.select_photo_btn);
this.select_photo_btn.setOnClickListener(this);
this.photo_iv = (ImageView) findViewById(R.id.photo_iv);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//拍照
case R.id.take_photo_btn:
picTyTakePhoto();
break;
//選擇圖庫
case R.id.select_photo_btn:
pickPhoto();
break;
}
}
/**
* 拍照獲取圖片
*/
private void picTyTakePhoto() {
//判斷SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"
/***
* 使用照相機(jī)拍照稼跳,拍照后的圖片會存放在相冊中盟庞。使用這種方式好處就是:獲取的圖片是拍照后的原圖,
* 如果不使用ContentValues存放照片路徑的話汤善,拍照后獲取的圖片為縮略圖有可能不清晰
*/
ContentValues values = new ContentValues();
photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, TAKE_PHOTO_CODE);
} else {
Toast.makeText(this, "內(nèi)存卡不存在", Toast.LENGTH_LONG).show();
}
}
/***
* 從相冊中取圖片
*/
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, SELECT_PIC_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
//從相冊取圖片什猖,有些手機(jī)有異常情況,請注意
if (requestCode == SELECT_PIC_CODE) {
if (null != data && null != data.getData()) {
photoUri = data.getData();
picPath = uriToFilePath(photoUri);
startPhotoZoom(photoUri, PHOTO_CROP_CODE);
} else {
Toast.makeText(this, "圖片選擇失敗", Toast.LENGTH_LONG).show();
}
} else if (requestCode == TAKE_PHOTO_CODE) {
String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
if (Build.VERSION.SDK_INT < 14) {
cursor.close();
}
}
if (picPath != null) {
photoUri = Uri.fromFile(new File(picPath));
startPhotoZoom(photoUri, PHOTO_CROP_CODE);
} else {
Toast.makeText(this, "圖片選擇失敗", Toast.LENGTH_LONG).show();
}
} else if (requestCode == PHOTO_CROP_CODE) {
if (photoUri != null) {
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
if (bitmap != null) {
//這里可以把圖片進(jìn)行上傳到服務(wù)器操作
photo_iv.setImageBitmap(bitmap);
}
}
}
}
}
private void startPhotoZoom(Uri uri, int REQUE_CODE_CROP) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的VIEW可裁剪
intent.putExtra("crop", "true");
// 去黑邊
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
// aspectX aspectY 是寬高的比例红淡,根據(jù)自己情況修改
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高像素
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//取消人臉識別功能
intent.putExtra("noFaceDetection", true);
//設(shè)置返回的uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//設(shè)置為不返回數(shù)據(jù)
intent.putExtra("return-data", false);
startActivityForResult(intent, REQUE_CODE_CROP);
}
private String uriToFilePath(Uri uri) {
//獲取圖片數(shù)據(jù)
String[] proj = {MediaStore.Images.Media.DATA};
//查詢
Cursor cursor = managedQuery(uri, proj, null, null, null);
//獲得用戶選擇的圖片的索引值
int image_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//返回圖片路徑
return cursor.getString(image_index);
}
}
整個選擇圖片并且裁剪的demo就寫好了不狮,其實(shí)是非常的簡單。