Android手機(jī)拍照或選取圖庫圖片作為頭像
更新時(shí)間:2015年06月18日 11:16:32 投稿:hebedich
這篇文章主要介紹了Android手機(jī)拍照或選取圖庫圖片作為頭像的相關(guān)資料,需要的朋友可以參考下
package zhangpgil.photo;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
public class MainActivity extends ActionBarActivity {
/* 頭像文件 */
private static final String IMAGE_FILE_NAME = "temp_head_image.jpg";
/* 請求識(shí)別碼 */
private static final int CODE_GALLERY_REQUEST = 0xa0;
private static final int CODE_CAMERA_REQUEST = 0xa1;
private static final int CODE_RESULT_REQUEST = 0xa2;
// 裁剪后圖片的寬(X)和高(Y),480 X 480的正方形。(生成bitmap貌似有時(shí)要報(bào)錯(cuò)?可試下把大小弄小點(diǎn))
private static int output_X = 480;
private static int output_Y = 480;
private ImageView headImage = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
headImage = (ImageView) findViewById(R.id.imageView);
Button buttonLocal = (Button) findViewById(R.id.buttonLocal);
buttonLocal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choseHeadImageFromGallery();
}
});
Button buttonCamera = (Button) findViewById(R.id.buttonCamera);
buttonCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choseHeadImageFromCameraCapture();
}
});
}
// 從本地相冊選取圖片作為頭像
private void choseHeadImageFromGallery() {
Intent intentFromGallery = new Intent();
// 設(shè)置文件類型
intentFromGallery.setType("image/*");
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST);
}
// 啟動(dòng)手機(jī)相機(jī)拍攝照片作為頭像
private void choseHeadImageFromCameraCapture() {
Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 判斷存儲(chǔ)卡是否可用,存儲(chǔ)照片文件
if (hasSdcard()) {
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment
.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
}
startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// 用戶沒有進(jìn)行有效的設(shè)置操作,返回
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplication(), "取消", Toast.LENGTH_LONG).show();
return;
}
switch (requestCode) {
case CODE_GALLERY_REQUEST:
cropRawPhoto(intent.getData());
break;
case CODE_CAMERA_REQUEST:
if (hasSdcard()) {
File tempFile = new File(
Environment.getExternalStorageDirectory(),
IMAGE_FILE_NAME);
cropRawPhoto(Uri.fromFile(tempFile));
} else {
Toast.makeText(getApplication(), "沒有SDCard!", Toast.LENGTH_LONG)
.show();
}
break;
case CODE_RESULT_REQUEST:
if (intent != null) {
setImageToHeadView(intent);
}
break;
}
super.onActivityResult(requestCode, resultCode, intent);
}
/**
* 裁剪原始的圖片
*/
public void cropRawPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 設(shè)置裁剪
intent.putExtra("crop", "true");
// aspectX , aspectY :寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX , outputY : 裁剪圖片寬高
intent.putExtra("outputX", output_X);
intent.putExtra("outputY", output_Y);
intent.putExtra("return-data", true);
startActivityForResult(intent, CODE_RESULT_REQUEST);
}
/**
* 提取保存裁剪之后的圖片數(shù)據(jù),并設(shè)置頭像部分的View
*/
private void setImageToHeadView(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
headImage.setImageBitmap(photo);
}
}
/**
* 檢查設(shè)備是否存在SDCard的工具方法
*/
public static boolean hasSdcard() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// 有存儲(chǔ)的SDCard
return true;
} else {
return false;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/buttonLocal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本地相冊選取頭像" />
<Button
android:id="@+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手機(jī)拍照選取頭像" />
</LinearLayout>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
- Android實(shí)現(xiàn)用戶頭像更換操作
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android一行代碼實(shí)現(xiàn)圓形頭像
- Android實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫與相機(jī)設(shè)置頭像并保存在本地及服務(wù)器
- Android實(shí)現(xiàn)個(gè)人資料頁面頭像背景模糊顯示包(狀態(tài)欄)
- Android頭像上傳功能的實(shí)現(xiàn)代碼(獲取頭像加剪切)
- Android實(shí)現(xiàn)IM多人員組合的群組頭像
相關(guān)文章
在Android上實(shí)現(xiàn)HttpServer的示例代碼
本篇文章主要介紹了在Android上實(shí)現(xiàn)HttpServer的示例代碼,實(shí)現(xiàn)Android本地的微型服務(wù)器,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
ViewPager的setOnPageChangeListener方法詳解
這篇文章主要介紹了ViewPager的setOnPageChangeListener方法詳解,非常不錯(cuò),具有參考解決借鑒價(jià)值,需要的朋友可以參考下2016-12-12
Android實(shí)現(xiàn)圖片壓縮(bitmap的六種壓縮方式)
Android中圖片是以bitmap形式存在的,這篇文章主要介紹了Android實(shí)現(xiàn)圖片壓縮(bitmap的六種壓縮方式),有興趣的可以了解一下。2017-02-02
Android自定義Drawable實(shí)現(xiàn)圓形和圓角
這篇文章主要為大家詳細(xì)介紹了Android自定義Drawable實(shí)現(xiàn)圓形和圓角,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android 三種實(shí)現(xiàn)定時(shí)器詳解及實(shí)現(xiàn)方法
本文主要介紹 Android 定時(shí)器的知識(shí)資料,這里整理了三種方法來實(shí)現(xiàn)定時(shí)器的方法,有需要的小伙伴可以參考下2016-09-09
Android中APK簽名工具之jarsigner和apksigner詳解
這篇文章主要給大家介紹了關(guān)于Android中APK簽名工具之jarsigner和apksigner的相關(guān)資料,文中介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06

