Matisse making address

Results the preview

Add the dependent

The compile 'com. Zhihu. Android: Matisse: 0.4.3'Copy the code

The premises must have the following dependencies:

Implementation 'com. Android. Support: appcompat - v7:26.1.0' implementation 'com. Android. Support: recyclerview - v7:26.1.0' Implementation 'com. Making. Bumptech. Glide: glide: 3.7.0'Copy the code

Among them, the first one depends on everyone’s project must have the second dependency to do the project should also use recyclerView, the third dependency is the dependency of picture loading, Matisse support Glide or Picasso as your picture loading engine, here is used Glide

Increase the permissions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Copy the code

Layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pick photos"
        android:background="#4169E1"
        android:textColor="#ffffff"
        />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Copy the code

Java file

public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE_CHOOSE = 23; Button button; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); textView = findViewById(R.id.textview); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Matisse.from(mainactivity.this).choose(mimeType.allof ())// Image type.countable (true)//true: select a number; MaxSelectable (5)// Maximum number.capture (true)// When selecting a photo, Whether to display photos. CaptureStrategy (new captureStrategy (true, "com. Example. Xx. Fileprovider")) / / parameter 1 true said photos stored in the Shared directory, false said stored in private directory; AndroidManifest = AndroidManifest; AndroidManifest = AndroidManifest ImageEngine (new GlideEngine())// imageEngine. ForResult (REQUEST_CODE_CHOOSE); / /}}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) { List<Uri> result = Matisse.obtainResult(data); textView.setText(result.toString()); }}}Copy the code

If you don’t take a photo, you can get the effect that I showed you at the beginning of this article if you want to take a photo, you have to add a Fileprovider to the application node in androidManifest.xml, the same level as the activity

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.xx.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_public">
            </meta-data>
        </provider>
Copy the code

Where com.example.xx. Change to your package name

Create a new XML folder in RES and add file_paths_public

The code for file_paths_public is

<paths>
    <external-path
        name="my_images"
        path="Pictures"/>
</paths>
Copy the code

After completing the above steps, you can use the photo function normally

Q&A

An error is reported when using Matisse and Glide 4.0.0 and later versions

Due to some changes in the way the Api is called since Glide4.0, some of the previous Api calls will fail. Changes to Api calls after Glide 4.0 can be found in the official documentation

Custom class MyGlideEngine, imitate class GlideEngine, re-implement ImageEngine

The following

import com.bumptech.glide.annotation.GlideModule; import com.bumptech.glide.module.AppGlideModule; import android.content.Context; import android.graphics.drawable.Drawable; import android.net.Uri; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.Priority; import com.zhihu.matisse.engine.ImageEngine; /** * {@link ImageEngine} implementation using Glide. */ public class MyGlideEngine implements ImageEngine { @Override public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) { GlideApp.with(context) .asBitmap() // some .jpeg files are actually gif .load(uri) .override(resize, resize) .centerCrop() .into(imageView); } @Override public void loadAnimatedGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) { GlideApp.with(context) .asBitmap() .load(uri) .placeholder(placeholder) .override(resize, resize) .centerCrop() .into(imageView); } @Override public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) { GlideApp.with(context) .load(uri) .override(resizeX, resizeY) .priority(Priority.HIGH) .into(imageView); } @Override public void loadAnimatedGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) { GlideApp.with(context) .load(uri) .override(resizeX, resizeY) .priority(Priority.HIGH) .into(imageView); } @Override public boolean supportAnimatedGif() { return true; }}Copy the code

Change to

.imageEngine(new MyGlideEngine())// imageEngineCopy the code

The use of about one GlideApp blog.csdn.net/u010356768/…

Links to other Matisse articles blog.csdn.net/qiaoshi96_b… www.jianshu.com/p/03567893a…