Android implements the method of obtaining pictures from the network for display and saving them to SD card

This paper describes the method of Android to obtain pictures from the network and save them to SD card. Share with you for your reference, as follows:

Question:

How to continuously obtain pictures and display them to achieve the effect of video?

code:

Note: 1. Permission problem

Permissions when network is involved:

Problem decomposition:

Question 1. How to get pictures from the network and display them:

Question 2. How to continuously display pictures:

Extension: how to save the acquired picture:

Problem 1 solution:

It seems that there are three options. In fact, there is only one model, changing soup without changing medicine. First, obtain a read picture stream through the unified resource location URL (unified resource location), then decompress it into a bitmap, and finally display it. The specific implementation code is as follows:

Select 1: direct class URL to open a stream, which is the most simple and practical.

Select 2: use class urlconnection to open the connection

Select 3: use class httpurlconnection to open the connection

Problem 2 solution:

It's easy to think of starting a timer and executing it every other time.

Another solution is to open a thread and use a sleep in the while loop to sleep for a while.

Save the acquired picture solution:

Saving an image naturally involves reading and writing files on the SD card. Here, the bitmap is written directly to the file. It's easy to think that streams must be used. However, we also need to understand the power of bitmapfactory class. Here we show the implementation process of saving file names in system time. One advantage is that they can be saved arbitrarily without considering coverage and cross-border issues.

Note: the bitmap used here is the bitmap generated above.

Seeing this problem feels like a comprehensive problem in high school. Decompose it into simple problems and solve each small problem, so complex problems can be solved naturally. I remember reading a post a few days ago. The theme is "when problems are decomposed into smaller problems, all problems become so simple, and all problems can be decomposed in this way." what is a cow is that when you encounter complex problems, you can keep clear thinking, analyze the problem process, and then decompose them into small enough problems, solve them one by one, and finally combine them. Just like seeing a small car with many parts, it's a little complicated. However, we continue to decompose: four wheels and shell, then the wheels are divided into steel rings and tire skins, and the tire skins are divided into inner tubes and outer tubes. Then what you have to do is find the manufacturer of tires and rims, buy these two components, and then use a third party or other tools to assemble them into wheels. Here, tires and rims are equivalent to Java classes, third-party or other assembly tools. Just like your code, assemble them with the engine to realize the function of running the car. Learn to decompose thinking, the most commonly used is dichotomy, of course, we have to analyze specific problems.

I hope this article will help you in Android programming.

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