Android take photos and get album pictures

Before I met all kinds of photos, such as obtaining album pictures, I went to Du Niang directly, or I copied the previous code and didn't summarize it well.

Don't ask Du Niang anymore. Don't look in a bunch of blogs...

----------------------------------------------I am the dividing line of the text-----------------------------------------------------------

One by one, let's call the mobile camera to take pictures (the simplest version):

This method is simple, but the obtained pictures are thumbnails, which are usually very fuzzy, and often do not meet our requirements. What we need is to obtain the original picture of the photos taken.

Generally, to obtain the original image obtained by photographing, we will first customize the image name and confirm the image storage location. Then, according to the image location URI, we can naturally obtain the original image bitmap. Code such as

The code should be very clear. If we define the image name and pass in the URI, we can get the URI in onactivityresult. Then can we get the bitmap directly from the URI? For example: Bitmap bitmap = mediastore Images. Media. getBitmap(getContentResolver(),photoUri); photoImageView. setImageBitmap(bitmap); It's right to do this. Unfortunately, most of our mobile phone cameras are very high-definition. If the photos taken are directly loaded into the mobile phone memory, I'm afraid they will be lalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalala

Then, the only thing we can think of is to start compressing pictures:

The above is all the code for taking photos with the camera. Of course, write_ EXTERNAL_ The permission of storage must be added.

Another thing that often happens is that after you take photos, the photos do not appear in the mobile phone album in time, but only after the mobile phone is restarted. In fact, we only need to send a broadcast to add pictures to the mobile phone album. The code is as follows:

The difficulty has passed. Relatively speaking, it's easy to select photos from photo albums. I believe I can understand it by looking at the code. I don't believe you see:

It's very simple. In onactivityresult(), we get the URI of the selected image according to the data, and then we can get the image path as long as we query in one step. With the photo path, there is no problem.

The whole demo is over here. You don't have to see the next content. Demo download link: download address

Q: what is an SD card? Because when I create the file, the directory file defaults to environment Getexternalstoragedirectory(), and the state needs to be detected before use, that is, this sentence environment getExternalStorageState(). Equals (environment. Media_mounted) and ask if the SD card is installed correctly. I used a new mobile phone, which is still installed correctly. A: the mobile phone has a built-in SD card, so even if I buy a new mobile phone, I can still detect the existence of SD card.

An interesting answer is that the built-in SD card is similar to the computer hard disk, and the external SD card is similar to the mobile hard disk, but it is not sure whether it is correct.

Q: when creating a file, what do several different directory files mean? Any difference?

A: look directly at the test results, environment Getexternalstoragedirectory() address: / storage / emulated / 0 environment Getexternalstoragepublicdirectory() address: / storage / emulated / 0 / pictures getexternalfilesdir() address: / storage / emulated / 0 / Android / data / com example. notificationapp/files/Pictures

Q: when selecting photos from an album, it seems that both intents can be used. What's the difference?

Answer: intent = new intent (intent. Action_pick); Intent intent=new Intent(Intent.ACTION_GET_CONTENT);

Both of them can choose photos from the album, but what's the difference? Well, I don't understand.

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