欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿微信選擇圖片和拍照功能

 更新時間:2016年11月28日 16:43:55   作者:志向遠大  
這篇文章主要為大家詳細介紹了Android仿微信選擇圖片和拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了 Android微信選擇圖片的具體代碼,和微信拍照功能,供大家參考,具體內容如下

1.Android6.0系統(tǒng),對于權限的使用都是需要申請,選擇圖片和拍照需要申請Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE這兩個權限。

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.通過圖片選擇器MultiImageSelector來管理: 選擇模式、最大選擇數量、是否啟動相機等功能。

3.點擊圖片選擇按鈕跳轉到MultiImageSelectorActivity類,其布局如下:(一個Toobar + 一個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.調用如下方法填充展示圖片的fragment(MultiImageSelectorFragment)。

   getSupportFragmentManager().beginTransaction()
     .add(R.id.image_grid, Fragment.instantiate(this, MultiImageSelectorFragment.class.getName(), bundle))
     .commit();

5.MultiImageSelectorFragment布局用gridview顯示從相冊獲取的圖片

<?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調用android.support.v4.app.LoaderManager.class類里面的LoaderCallbacks方法,等加載完成后給mImageAdapter設置數據。

mImageAdapter.setData(images);

7.當允許拍照的時候,顯示拍照按鈕,調用系統(tǒng)相機功能。

 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);
    }
   }
  });

調用相機功能

 /**
  * 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微信開發(fā)教程匯總》,歡迎大家學習閱讀。

源碼下載:http://xiazai.jb51.net/201611/yuanma/AndroidselectPicture(jb51.net).rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android進階手寫IPC通信框架告別繁瑣AIDL

    Android進階手寫IPC通信框架告別繁瑣AIDL

    這篇文章主要為大家介紹了Android進階手寫IPC通信框架告別繁瑣AIDL實現詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 詳解Android studio實現語音轉文字功能

    詳解Android studio實現語音轉文字功能

    這篇文章主要介紹了如何通過Android studio調用科大訊飛的語音轉文字功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-03-03
  • Android控件CardView實現卡片效果

    Android控件CardView實現卡片效果

    這篇文章主要為大家詳細介紹了Android控件CardView實現卡片效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Suspend函數與回調的互相轉換示例詳解

    Suspend函數與回調的互相轉換示例詳解

    這篇文章主要為大家介紹了Suspend函數與回調的互相轉換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Android實現密碼明密文切換(小眼睛)

    Android實現密碼明密文切換(小眼睛)

    這篇文章主要為大家詳細介紹了Android實現密碼明密文切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android?NotificationListenerService?通知服務原理解析

    Android?NotificationListenerService?通知服務原理解析

    這篇文章主要為大家介紹了Android?NotificationListenerService?通知服務原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android編程實現WebView自適應全屏方法小結

    Android編程實現WebView自適應全屏方法小結

    這篇文章主要介紹了Android編程實現WebView自適應全屏方法,結合實例形式總結了三種常用的WebView自適應全屏實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • Android 判斷是否有外網連接

    Android 判斷是否有外網連接

    本文給大家分享的是使用Android實現判斷是否有外網鏈接,有需要的小伙伴可以參考下。
    2016-02-02
  • Android使用SoundPool播放音效實例

    Android使用SoundPool播放音效實例

    這篇文章主要為大家詳細介紹了Android使用SoundPool播放音效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android入門之利用Spinner實現彈出選擇對話框

    Android入門之利用Spinner實現彈出選擇對話框

    這篇文章主要為大家詳細介紹了Android里如何巧用Spinner做彈出選擇對話框,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下
    2022-11-11

最新評論