Specific implementation of obtaining pictures from Android system Gallery

preface

In Android applications, there are often scenes that need to use the pictures stored on the device, and it is undoubtedly very inconvenient to get them directly from the path. Therefore, it is generally recommended to call the gallery application of the system, select the image, and then use it. This blog will explain how to get pictures through the system Gallery in Android.

Gallery application

Android has many built-in apps, and gallery is a gallery for operating pictures on the device. It will actively scan the pictures stored on the device when it is turned on, and you can use gallery to operate them. Now that you want to use Gallery, take a look at its androidmanifest. XML manifest file.

The above is part of the code in the androidmanifest.xml file of the gallery, which shows the ImageGallery. It can be seen from many intent filters that "Android. Intent. Action. Pick" should be used to select images. It has two minitypes, "image / *" is used to obtain images, and "video / *" is used to obtain videos. The strings of many actions in Android are actually encapsulated in the intent class. Android.intent.action.pick is no exception. It is intent.action_ PICK。

Now that you know the action to start the gallery, look at the source code of imagegallery.java to find the return value after the selected image.

Part of the above source code of imagegallery.java can be seen from the setresult () method that the returned intent contains the URI of the selected image. It is a content provider starting with content: / /. If the extra of the past intent contains an image thumbnail with the name of "return data" and the value is true, the image thumbnail with the name of "data" will be written to the extra.

Gallery get picture demo

Now that you know the action to start the gallery and how it returns the selected data, let's use a simple demo to demonstrate how to obtain pictures from the system gallery and display the obtained pictures in an ImageView of the interface.

Effect display:

summary

Here, this blog basically explains how to use the system gallery on Android to obtain pictures. In fact, the function implementation is very simple. The main thing is to note that the action and minitype should not be written wrong, and the return value is a URI. Although more and more commercial applications that need to use pictures are developing the function of obtaining device pictures, using the gallery provided by the system to obtain them is a solution to quickly realize the function. For convenience, the gallery source code of the system will also be packaged into the source code. You can download it if necessary.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>