SoFunction
Updated on 2025-04-05

Android imitation WeChat to select pictures and take photos

The examples in this article share with you the specific code of Android WeChat selection and the WeChat photo function for your reference. The specific content is as follows

1. Android 6.0 system requires application for the use of permissions. You need to apply for the selection of pictures and taking photos. READ_EXTERNAL_STORAGE permissions.

if ((this, ) != PackageManager.PERMISSION_GRANTED) {
   ((Activity) this,
     new String[] { , .READ_EXTERNAL_STORAGE},
     REQUEST_STORAGE_READ_ACCESS_PERMISSION);
  }

2.Manage through the MultiImageSelector image selector: select mode, maximum number of selections, whether to enable camera and other functions.

3. Click the image selection button to jump to the MultiImageSelectorActivity class, which is layout as follows: (A Toobar + A FrameLayout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 xmlns:app="/apk/res-auto"
 android:orientation="vertical"
 android:background="#181819"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <.
  android:
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/mis_actionbar_color"
  app:theme="@style/"
  android:minHeight="?android:attr/actionBarSize">

  <Button
   android:
   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" />

 </.>

 <FrameLayout
  android:
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</LinearLayout>

4. Call the following method to fill the fragment (MultiImageSelectorFragment) of the display image.

   getSupportFragmentManager().beginTransaction()
     .add(.image_grid, (this, (), bundle))
     .commit();

Layout using gridview to display pictures obtained from albums

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:andro
 xmlns:tools="/tools"
 android:background="@android:color/black"
 android:layout_width="match_parent"
 android:layout_height="match_parent"&gt;

 &lt;GridView
  android:
  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"/&gt;

 &lt;RelativeLayout
  android:clickable="true"
  android:
  android:background="#cc000000"
  android:layout_alignParentBottom="true"
  android:layout_width="match_parent"
  android:layout_height="?android:attr/actionBarSize"&gt;

  &lt;Button
   android:
   android:paddingLeft="16dp"
   android:paddingRight="16dp"
   android:layout_centerVertical="true"
   android:textColor="@color/mis_folder_text_color"
   tools:text="All pictures"
   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" /&gt;

  &lt;/RelativeLayout&gt;

&lt;/RelativeLayout&gt;

6 Call the LoaderCallbacks method in the class, and set data for mImageAdapter after loading.

(images);

7. When taking pictures is allowed, display the photo button and call the system camera function.

 (new () {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    if (()) {
     if (i == 0) {
      showCameraAction();
     } else {
      Image image = (Image) ().getItem(i);
      selectImageFromGrid(image, mode);
     }
    } else {
     Image image = (Image) ().getItem(i);
     selectImageFromGrid(image, mode);
    }
   }
  });

Calling the camera function

 /**
  * Open camera
  */
 private void showCameraAction() {
  if((getContext(), .WRITE_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED){
   requestPermission(.WRITE_EXTERNAL_STORAGE,
     getString(.mis_permission_rationale_write_storage),
     REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
  }else {
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   if ((getActivity().getPackageManager()) != null) {
    try {
     mTmpFile = (getActivity());
    } catch (IOException e) {
     ();
    }
    if (mTmpFile != null && ()) {
     (MediaStore.EXTRA_OUTPUT, (mTmpFile));
     startActivityForResult(intent, REQUEST_CAMERA);
    } else {
     (getActivity(), .mis_error_image_not_exist, Toast.LENGTH_SHORT).show();
    }
   } else {
    (getActivity(), .mis_msg_no_camera, Toast.LENGTH_SHORT).show();
   }
  }
 }

Select a picture

 /**
  * notify callback
  * @param image image data
  */
 private void selectImageFromGrid(Image image, int mode) {
  if(image != null) {
   if(mode == MODE_MULTI) {
    if (()) {
     ();
     if (mCallback != null) {
      ();
     }
    } else {
     if(selectImageCount() == ()){
      (getActivity(), .mis_msg_amount_limit, Toast.LENGTH_SHORT).show();
      return;
     }
     ();
     if (mCallback != null) {
      ();
     }
    }
    (image);
   }else if(mode == MODE_SINGLE){
    if(mCallback != null){
     ();
    }
   }
  }
 }

This article has been compiled intoAndroid WeChat development tutorial summary》, everyone is welcome to learn and read.

Source code download:http://xiazai./201611/yuanma/AndroidselectPicture().rar

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.