Example of using Android OSS to upload pictures

preface

The uploaded pictures in the previous project were uploaded to Alibaba instead. Record the implementation process for later viewing.

Reference: Official Documents

to configure

Android studio add dependency

Directly import the jar package (applicable to both Android studio and eclipse)

1. Download the SDK package on the official website. 2. Unzip and get the jar package, which currently includes aliyun-oss-sdk-android-x.x.x.jar, okhttp-3.x.x.jar and okio-1.x.x.jar. 3. Import the above three jar packages into the LIBS directory

Permission setting

Ensure that these permissions are configured in the androidmanifest.xml file, otherwise the SDK will not work properly.

Confusion settings

Implementation process

Firstly, for security reasons, STS authentication mode is adopted, and the data to be used is obtained from the background and then applied to the foreground.

1. Create an ossclient (named ossservice here)

Ossclient is an Android client of OSS service. It provides a series of methods for callers to operate and manage buckets and objects.

2. Method of implementing ossservice (in activity)

3. Overload the ossfederationcredentialprovider to generate its own function to obtain STS (generally, the token is automatically obtained, which is written here. In the getfederationtoken () method, just tell it the rules for obtaining the token)

1> Official demo code (automatically update token)

2> Your own code (because all your data is obtained from the background, and combined with rxjava, you didn't think of a way to return data, so you can manually update the token)

To manually update a token:

First, the value of the token is stored in myapp. The first time you enter the interface that needs a token, you first obtain the value of the token, update the value in myapp and record the current time. If you enter any interface that needs a token again, you will judge whether the time has expired. If it expires, you will re request the token to update the value of the token.

4. Instantiate the ossclient and call the upload image method

5. Callback processing picture logic

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