Android實(shí)現(xiàn)拍照、選擇相冊(cè)圖片并裁剪功能
通過(guò)拍照或相冊(cè)中獲取圖片,并進(jìn)行裁剪操作,然后把圖片顯示到ImageView上。
當(dāng)然也可以上傳到服務(wù)器(項(xiàng)目中絕大部分情況是上傳到服務(wù)器),參考網(wǎng)上資料及結(jié)合項(xiàng)目實(shí)際情況,
測(cè)試了多款手機(jī)暫時(shí)沒(méi)有發(fā)現(xiàn)嚴(yán)重問(wèn)題。代碼有注釋?zhuān)苯淤N代碼:
public class UploadPicActivity 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;
//使用相冊(cè)中的圖片
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_upload_pic);
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;
//選擇圖庫(kù)
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ī)拍照,拍照后的圖片會(huì)存放在相冊(cè)中。使用這種方式好處就是:獲取的圖片是拍照后的原圖,
* 如果不實(shí)用ContentValues存放照片路徑的話(huà),拍照后獲取的圖片為縮略圖有可能不清晰
*/
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();
}
}
/***
* 從相冊(cè)中取圖片
*/
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) {
//從相冊(cè)取圖片,有些手機(jī)有異常情況,請(qǐng)注意
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);
}
}
}
}
}
/**
* @param
* @description 裁剪圖片
* @author ldm
* @time 2016/11/30 15:19
*/
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è)置在開(kāi)啟的Intent中設(shè)置顯示的VIEW可裁剪
intent.putExtra("crop", "true");
// 去黑邊
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
// aspectX aspectY 是寬高的比例,根據(jù)自己情況修改
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 2);
// outputX outputY 是裁剪圖片寬高像素
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 400);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//取消人臉識(shí)別功能
intent.putExtra("noFaceDetection", true);
//設(shè)置返回的uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//設(shè)置為不返回?cái)?shù)據(jù)
intent.putExtra("return-data", false);
startActivityForResult(intent, REQUE_CODE_CROP);
}
/**
* @param
* @description 把Uri轉(zhuǎn)換為文件路徑
* @author ldm
* @time 2016/11/30 15:22
*/
private String uriToFilePath(Uri uri) {
//獲取圖片數(shù)據(jù)
String[] proj = {MediaStore.Images.Media.DATA};
//查詢(xún)
Cursor cursor = managedQuery(uri, proj, null, null, null);
//獲得用戶(hù)選擇的圖片的索引值
int image_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//返回圖片路徑
return cursor.getString(image_index);
}
}
布局文件長(zhǎng)這樣:
<?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>
最后不要忘記在AndroidManifest.xml中添加UploadPicActivity及權(quán)限:
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android的activity跳轉(zhuǎn)到另一個(gè)activity
這篇文章主要介紹了android實(shí)現(xiàn)從一個(gè)activity跳轉(zhuǎn)到另一個(gè)activity中去2013-11-11
Android 防止過(guò)快(多次)點(diǎn)擊的實(shí)現(xiàn)方法
很多用戶(hù)經(jīng)常會(huì)出現(xiàn)過(guò)快且多次點(diǎn)擊同一按鈕的情況,本篇文章主要介紹了Android 防止過(guò)快點(diǎn)擊的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android中SQLite數(shù)據(jù)庫(kù)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于Android中SQLite數(shù)據(jù)庫(kù)知識(shí)點(diǎn)總結(jié),有需要的朋友們跟著學(xué)習(xí)下。2019-02-02
Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息
這篇文章主要介紹了Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息的使用方法,代碼很簡(jiǎn)單2014-01-01
gradle配置國(guó)內(nèi)鏡像的實(shí)現(xiàn)
這篇文章主要介紹了gradle配置國(guó)內(nèi)鏡像的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
android自定義ViewPager水平滑動(dòng)彈性效果
這篇文章主要為大家詳細(xì)介紹了android自定義ViewPager水平滑動(dòng)彈性,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
本篇文章主要介紹了Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer),具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09

