Seven cattle cloud storage is used for image upload and download in Android development

Image storage in Android development is a time-consuming and land-consuming thing, and using the third-party qiniu cloud can well solve these worries. Recently, I am also learning qiniu SDK to record the use process here for future use.

First, let's talk about the storage principle of qiniu cloud. The picture above is the official schematic diagram. Of course, the description is relatively clear. It can be seen that uploading pictures can be divided into five steps:

1. The client user logs in to the account system of the app;

2. Before the client uploads files, it needs to apply to the service server for the upload voucher of qiniu, which is generated by the service server using the service SDK provided by qiniu;

3. The client uses the client SDK provided by qiniu to call the upload method to upload files. The upload method must include the upload voucher and file content (since qiniu allows files with a size of 0, it is recommended to check the file size before uploading. If the business does not allow files with a size of 0, you need to check it yourself);

4. After the client file is uploaded to qiniu, the optional operation is qiniu callback service server (that is, qiniu sends a post request for file related information to the callback address specified in the upload strategy);

5. The business server replies to the callback request of qiniu and gives the reply content in JSON format (it must be a reply in JSON format), which will be forwarded by qiniu to the client;

Well, the operation principle of qiniu cloud is clear. It's not very troublesome to understand it carefully. Let's start the integration operation.

1、 Download the official SDK

Invalid Sign

Don't worry. In addition to relying on qiniu Android SDK, you should also rely on happy DNS, okhttp and Android async http. There are four dependent packages in total. Here's a tip. If you don't think it's troublesome to download those things, you can download the official demo, and then put all the dependency packages in your own project. Of course, the premise of doing so is that you have to distinguish what is what.

2、 Add permissions to manifest file

Note: if you use Android 5.0 and above, you need to apply for permission in the code.

3、 Define variables

Before writing the upload and download code, we need to define the following variables.

4、 Upload pictures

Qiniu server can upload three types, including byte [] type pictures, string type file paths, and file type files;

(1) Request token from server

(2) Initialize upload parameters

(3) Start the asynchronous thread and upload the picture file

5、 Download pictures

The SDK does not provide functional interfaces related to downloading files, because file downloading is a standard HTTP get process. Developers only need to understand the composition format of the resource URI to easily build the resource URI, and add the download certificate when necessary to obtain the corresponding resources using the HTTP get request@ H_ 419_ 423@

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