Android 項(xiàng)目實(shí)戰(zhàn)之頭像選擇功能
一、圖片選擇
1.1 目標(biāo)
1.實(shí)現(xiàn)如圖所示功能:能夠出現(xiàn)相冊和相機(jī)選項(xiàng)
2.能夠?qū)x擇的圖片進(jìn)行裁剪
1.2 功能實(shí)現(xiàn)
1.2.1 Intent工具類封裝
封裝圖片選擇和圖片裁剪的工具類
/**
* 選擇圖片(從相冊或相機(jī))
* @param uri 相機(jī)存儲uri
* @return
*/
public static Intent getPhotoSelectIntent(Uri uri){
Intent take = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
take.addCategory(Intent.CATEGORY_DEFAULT);
take.putExtra(MediaStore.EXTRA_OUTPUT, uri);
Intent pics = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent chose= Intent.createChooser(pics,"選擇圖片");
chose.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{take});
return chose;
}
/**
* 圖片裁剪
* @param inputUri 需要裁剪的圖片
* @param outputUri 裁剪后存儲位置
* @param width 裁剪寬度
* @param height 裁剪高度
* @return
*/
public static Intent getImageCropIntent(Uri inputUri, Uri outputUri, int width, int height) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(inputUri, "image/*");
// 下面這個crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的VIEW可裁剪
intent.putExtra("crop", "true");
intent.putExtra("scale", true); // 去黑邊
intent.putExtra("scaleUpIfNeeded", true); // 去黑邊
// aspectX aspectY 裁剪框?qū)捀弑壤?
intent.putExtra("aspectX", width); // 輸出是X方向的比例
intent.putExtra("aspectY", height);
// outputX outputY 輸出圖片寬高,切忌不要再改動下列數(shù)字,會卡死
intent.putExtra("outputX", width); // 輸出X方向的像素
intent.putExtra("outputY", height);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("return-data", false); // 設(shè)置為不返回?cái)?shù)據(jù)
return intent;
}
1.2.2 添加點(diǎn)擊圖片選擇事件
b.ivAvatar.setOnClickListener {
mTakePhotoFile = File(getPicPath() + File.separator + System.currentTimeMillis() + ".jpeg")
val uri = Uri.fromFile(mTakePhotoFile)
startActivityForResult(IntentUtils.getPhotoSelectIntent( uri), TAKE_PHOTO_REQ)
}
1.2.3 處理圖片選擇和裁剪反饋
圖片裁剪所需的Uri類似: content:// 的形式,因此需要封裝一個獲取content Uri的工具類
public static Uri getContentUri(Context context, File file) {
String filePath = file.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (file.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
處理反饋結(jié)果
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode != -1) {
return
}
when (requestCode) {
TAKE_PHOTO_REQ -> {
// 處理圖片選擇結(jié)果
mCutPhotoFile = File(getPicPath() + File.separator + "avatar_" + System.currentTimeMillis() + "jpeg")
val cutUri = Uri.fromFile(mCutPhotoFile)
if (data != null){
startActivityForResult(IntentUtils.getImageCropIntent(data.data, cutUri, 200, 200), CUT_PHOTO_REQ)
} else {
val uri = UriUtils.getContentUri(applicationContext, mTakePhotoFile)
startActivityForResult(IntentUtils.getImageCropIntent(uri, cutUri, 200, 200), CUT_PHOTO_REQ)
}
}
CUT_PHOTO_REQ -> {
// 處理圖片裁剪結(jié)果
}
}
}
1.2.4 Android 7.0適配
1. res/xml/provider_paths.xml路徑自行更換
<paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path path="Android/data/com/example/sunmoon/images" name="sdcard_files" /> <external-files-path path="Android/data/com/example/sunmoon/images" name="camera_has_sdcard"/> <files-path path="Android/data/com/example/sunmoon/other" name="camera_no_sdcard"/> <external-path path="Android/data/com/example/sunmoon" name="files_root" /> <external-path path="." name="external_storage_root" /> </paths>
2. manifests配置包名自行更換
...
<application>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.sunmoon.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
...
</application>
...
總結(jié)
以上所述是小編給大家介紹的Android 項(xiàng)目實(shí)戰(zhàn)之頭像選擇功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫與相機(jī)設(shè)置頭像并保存在本地及服務(wù)器
- Android實(shí)現(xiàn)個人資料頁面頭像背景模糊顯示包(狀態(tài)欄)
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android中使用CircleImageView和Cardview制作圓形頭像的方法
- Android獲取聯(lián)系人頭像的方法
- Android實(shí)現(xiàn)用戶頭像更換操作
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
相關(guān)文章
解決Android Studio 出現(xiàn)“Cannot resolve symbo
今天在調(diào)試的時候,Android Studio報(bào)了一個莫名其妙的錯誤Cannot resolve symbol'R'讓人不知所措,因?yàn)檫@東西根本不歸我管啊,怎么會出現(xiàn) Cannot resolve symbol 這種錯誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下2023-03-03
用Android?studio實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了用Android?studio實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
BootStrapValidator與My97日期校驗(yàn)的實(shí)例代碼
這篇文章給大家介紹了bootstrapvalidator與my97日期校驗(yàn)的實(shí)例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01
Android編程開發(fā)之性能優(yōu)化技巧總結(jié)
這篇文章主要介紹了Android編程開發(fā)之性能優(yōu)化技巧,較為詳細(xì)的總結(jié)了Android編程中關(guān)于性能優(yōu)化的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android編程實(shí)現(xiàn)類似天氣預(yù)報(bào)圖文字幕垂直滾動效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)類似天氣預(yù)報(bào)圖文字幕垂直滾動效果的方法,涉及Android基于布局及事件響應(yīng)實(shí)現(xiàn)圖文滾動效果的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android組件實(shí)現(xiàn)長按彈出上下文菜單功能的方法
這篇文章主要介紹了Android組件實(shí)現(xiàn)長按彈出上下文菜單功能的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)長按彈出上下文菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07

