Android通過(guò)手機(jī)拍照或從本地相冊(cè)選取圖片設(shè)置頭像
像微信、QQ、微博等社交類的APP,通常都有設(shè)置頭像的功能,設(shè)置頭像通常有兩種方式:
1、讓用戶通過(guò)選擇本地相冊(cè)之類的圖片庫(kù)中已有的圖像,裁剪后作為頭像。
2、讓用戶啟動(dòng)手機(jī)的相機(jī)拍照,拍完照片后裁剪,然后作為頭像。
我現(xiàn)在寫一個(gè)簡(jiǎn)單的完整代碼例子,說(shuō)明如何在android中實(shí)現(xiàn)上述兩個(gè)頭像設(shè)置功能。
MainActivity.Java文件:
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";
/* 請(qǐng)求識(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的正方形。
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();
}
});
}
// 從本地相冊(cè)選取圖片作為頭像
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) {
// 用戶沒(méi)有進(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(), "沒(méi)有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;
}
}
}
布局文件有三個(gè)組件:放置頭像的ImageView,兩個(gè)Button,其中一個(gè)Button觸發(fā)從本地相冊(cè)選取圖片作為頭像的操作時(shí)間;另外一個(gè)Button觸發(fā)手機(jī)拍攝照片作為頭像的操作事件。
activity_main.xml:
<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="本地相冊(cè)選取頭像" />
<Button
android:id="@+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手機(jī)拍照選取頭像" />
</LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android啟動(dòng)相機(jī)拍照并返回圖片
- Android拍照保存在系統(tǒng)相冊(cè)不顯示的問(wèn)題解決方法
- Android仿微信發(fā)表說(shuō)說(shuō)實(shí)現(xiàn)拍照、多圖上傳功能
- Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能
- android 拍照和上傳的實(shí)現(xiàn)代碼
- Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能
- Android開(kāi)發(fā)從相冊(cè)中選取照片的示例代碼
- android實(shí)現(xiàn)拍照或從相冊(cè)選取圖片
相關(guān)文章
Android?Gradle?插件自定義Plugin實(shí)現(xiàn)注意事項(xiàng)
這篇文章主要介紹了Android?Gradle?插件自定義Plugin實(shí)現(xiàn)注意事項(xiàng),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-06-06
Android11及以上文件讀寫權(quán)限申請(qǐng)?jiān)敿?xì)介紹
安卓11改變了此前安卓系統(tǒng)對(duì)于文件管理的規(guī)則,在安卓 11 上,文件讀寫變成了特殊權(quán)限,下面這篇文章主要給大家介紹了關(guān)于Android11及以上文件讀寫權(quán)限申請(qǐng)的相關(guān)資料,需要的朋友可以參考下2022-08-08
Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android賬號(hào)注冊(cè)過(guò)程中實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Flutter利用ORM框架管理數(shù)據(jù)庫(kù)詳解
使用?ORM?框架最大的好處是簡(jiǎn)化了數(shù)據(jù)庫(kù)維護(hù)的代碼量,使得我們可以專注于業(yè)務(wù)代碼實(shí)現(xiàn)。本篇,我們看看如何使用ORM框架管理數(shù)據(jù)庫(kù)版本遷移,需要的可以參考一下2023-04-04
Android開(kāi)發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二)
我們?cè)趯?shí)際開(kāi)發(fā)中,有的時(shí)候需要儲(chǔ)存或者備份比較復(fù)雜的數(shù)據(jù)。這些數(shù)據(jù)的特點(diǎn)是,內(nèi)容多、結(jié)構(gòu)大,比如短信備份等,通過(guò)本文給大家介紹Android開(kāi)發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二),對(duì)android數(shù)據(jù)存儲(chǔ)方式相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
Android編程實(shí)現(xiàn)可滑動(dòng)的開(kāi)關(guān)效果(附demo源碼下載)
這篇文章主要介紹了Android編程實(shí)現(xiàn)可滑動(dòng)的開(kāi)關(guān)效果,涉及Android的布局與控件設(shè)置技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04
手把手教你用ViewPager自定義實(shí)現(xiàn)Banner輪播
這篇文章主要手把手教你用ViewPager自定義實(shí)現(xiàn)Banner輪播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

