Android仿微信選擇圖片和拍照功能
本文實(shí)例為大家分享了 Android微信選擇圖片的具體代碼,和微信拍照功能,供大家參考,具體內(nèi)容如下
1.Android6.0系統(tǒng),對(duì)于權(quán)限的使用都是需要申請(qǐng),選擇圖片和拍照需要申請(qǐng)Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE這兩個(gè)權(quán)限。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) this,
new String[] { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_STORAGE_READ_ACCESS_PERMISSION);
}
2.通過(guò)圖片選擇器MultiImageSelector來(lái)管理: 選擇模式、最大選擇數(shù)量、是否啟動(dòng)相機(jī)等功能。
3.點(diǎn)擊圖片選擇按鈕跳轉(zhuǎn)到MultiImageSelectorActivity類,其布局如下:(一個(gè)Toobar + 一個(gè)FrameLayout)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:background="#181819" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/mis_actionbar_color" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:minHeight="?android:attr/actionBarSize"> <Button android:id="@+id/commit" android:background="@drawable/mis_action_btn" android:minHeight="1dp" android:minWidth="1dp" android:layout_marginRight="16dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:textColor="@color/mis_default_text_color" android:textSize="14sp" android:layout_gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.Toolbar> <FrameLayout android:id="@+id/image_grid" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
4.調(diào)用如下方法填充展示圖片的fragment(MultiImageSelectorFragment)。
getSupportFragmentManager().beginTransaction()
.add(R.id.image_grid, Fragment.instantiate(this, MultiImageSelectorFragment.class.getName(), bundle))
.commit();
5.MultiImageSelectorFragment布局用gridview顯示從相冊(cè)獲取的圖片
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="@android:color/black" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:id="@+id/grid" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="@dimen/mis_space_size" android:verticalSpacing="@dimen/mis_space_size" android:paddingBottom="?android:attr/actionBarSize" android:clipToPadding="false" android:numColumns="3"/> <RelativeLayout android:clickable="true" android:id="@+id/footer" android:background="#cc000000" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize"> <Button android:id="@+id/category_btn" android:paddingLeft="16dp" android:paddingRight="16dp" android:layout_centerVertical="true" android:textColor="@color/mis_folder_text_color" tools:text="所有圖片" android:textSize="16sp" android:gravity="center_vertical" android:drawableRight="@drawable/mis_text_indicator" android:drawablePadding="5dp" android:background="@null" android:singleLine="true" android:ellipsize="end" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RelativeLayout> </RelativeLayout>
6調(diào)用android.support.v4.app.LoaderManager.class類里面的LoaderCallbacks方法,等加載完成后給mImageAdapter設(shè)置數(shù)據(jù)。
mImageAdapter.setData(images);
7.當(dāng)允許拍照的時(shí)候,顯示拍照按鈕,調(diào)用系統(tǒng)相機(jī)功能。
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (mImageAdapter.isShowCamera()) {
if (i == 0) {
showCameraAction();
} else {
Image image = (Image) adapterView.getAdapter().getItem(i);
selectImageFromGrid(image, mode);
}
} else {
Image image = (Image) adapterView.getAdapter().getItem(i);
selectImageFromGrid(image, mode);
}
}
});
調(diào)用相機(jī)功能
/**
* Open camera
*/
private void showCameraAction() {
if(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
getString(R.string.mis_permission_rationale_write_storage),
REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
}else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
try {
mTmpFile = FileUtils.createTmpFile(getActivity());
} catch (IOException e) {
e.printStackTrace();
}
if (mTmpFile != null && mTmpFile.exists()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
startActivityForResult(intent, REQUEST_CAMERA);
} else {
Toast.makeText(getActivity(), R.string.mis_error_image_not_exist, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), R.string.mis_msg_no_camera, Toast.LENGTH_SHORT).show();
}
}
}
選擇圖片
/**
* notify callback
* @param image image data
*/
private void selectImageFromGrid(Image image, int mode) {
if(image != null) {
if(mode == MODE_MULTI) {
if (resultList.contains(image.path)) {
resultList.remove(image.path);
if (mCallback != null) {
mCallback.onImageUnselected(image.path);
}
} else {
if(selectImageCount() == resultList.size()){
Toast.makeText(getActivity(), R.string.mis_msg_amount_limit, Toast.LENGTH_SHORT).show();
return;
}
resultList.add(image.path);
if (mCallback != null) {
mCallback.onImageSelected(image.path);
}
}
mImageAdapter.select(image);
}else if(mode == MODE_SINGLE){
if(mCallback != null){
mCallback.onSingleImageSelected(image.path);
}
}
}
}
本文已被整理到了《Android微信開(kāi)發(fā)教程匯總》,歡迎大家學(xué)習(xí)閱讀。
源碼下載:http://xiazai.jb51.net/201611/yuanma/AndroidselectPicture(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- 淺談?wù)凙ndroid 圖片選擇器
- 分享實(shí)現(xiàn)Android圖片選擇的兩種方式
- Android選擇圖片或拍照?qǐng)D片上傳到服務(wù)器
- Android仿微信照片選擇器實(shí)現(xiàn)預(yù)覽查看圖片
- Android實(shí)現(xiàn)圖片選擇上傳功能實(shí)例
- Android實(shí)現(xiàn)本地圖片選擇及預(yù)覽縮放效果
- Android拍照或從圖庫(kù)選擇圖片并裁剪
- android完美實(shí)現(xiàn) 拍照 選擇圖片 剪裁等代碼分享
- Android圖片或拍照選擇圖片功能實(shí)例代碼
相關(guān)文章
Android 使用Vitamio打造自己的萬(wàn)能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實(shí)例代碼以便大家參考學(xué)習(xí),希望能幫助開(kāi)發(fā)Android視頻播放的朋友2016-07-07
Android仿京東淘寶自動(dòng)無(wú)限循環(huán)輪播控件思路詳解
在App的開(kāi)發(fā)中,很多的時(shí)候都需要實(shí)現(xiàn)類似京東淘寶一樣的自動(dòng)無(wú)限輪播的廣告欄,這里小編寫(xiě)了一個(gè),分享到腳本之家平臺(tái)供大家參考2017-04-04
Jetpack?Compose入門(mén)基礎(chǔ)全面精講
開(kāi)始布局部分。這部分我個(gè)人感覺(jué)沒(méi)有必要每個(gè)組件、屬性都詳細(xì)說(shuō)到,否則篇幅會(huì)很長(zhǎng)。建立起Compose中的組件與?Android?Views的一個(gè)對(duì)應(yīng)關(guān)系就夠了。具體還是需要在實(shí)際的使用中去熟悉2022-10-10
Android WorkManager實(shí)現(xiàn)后臺(tái)定時(shí)任務(wù)流程詳解
WorkManager是Android Jetpack的一個(gè)強(qiáng)大的組件,用于處理后臺(tái)耗時(shí)任務(wù)。后臺(tái)任務(wù)可以是一次性的,也可以是重復(fù)的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
Android 進(jìn)度條按鈕ProgressButton的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 進(jìn)度條按鈕實(shí)現(xiàn)(ProgressButton)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-10-10
Android SharedPreferences存儲(chǔ)的正確寫(xiě)法
這篇文章主要介紹了Android SharedPreferences存儲(chǔ)的正確寫(xiě)法的相關(guān)資料,需要的朋友可以參考下2017-06-06

