Implementation method of Android obtaining local album pictures and taking pictures

Requirements: find pictures from local photo albums, or get pictures by calling the system camera.

Error prone areas:

1. When we specify the URI path of the photo, we cannot use data. Getdata(); Instead, you should get the URI directly (using global variables or other methods) and set it to ImageView

imageView.setImageURI(uri);

2. I found that the photos taken by the front camera of the mobile phone are only a few hundred KB, and imageview.setimageuri (URI) is directly used; There is no big problem, but the photos taken by the rear camera are relatively large. ImageView. Setimageuri (URI) is used at this time; Out of memory (OOM) errors are easy to occur. We need to convert the URI to bitmap, compress the bitmap, and then use imageview.setimagebitmap (bitmap); To display the picture.

3. After the photos are stored in the SD card, they cannot appear in the system album immediately, so we need to send a broadcast to remind the album to update the photos.

4. The sharereference is used here. You should pay attention to removing the cache after it is used up.

code:

MainActivity:

ImageTools:

AndroidMainfest.xml:

activity_ main.xml:

design sketch:

Some people may ask how to click to take photos on Android 6.0 to flash back. That's because the maximum SDK version I set is greater than 23, and I haven't handled the runtime permissions yet. Maybe I'll deal with this problem in my next blog. Thank you for browsing. I hope it will help you!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>