您好,登錄后才能下訂單哦!
Android中阿里云OSS文件上傳的方法?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
一、項(xiàng)目導(dǎo)入阿里云oss的SDK
方式1:在build文件添加依賴(博主和官方一致推薦)
dependencies { //阿里云oss implementation 'com.aliyun.dpa:oss-android-sdk:+' }
方式2:源碼編譯jar包 源碼傳送門(mén):官方阿里云oss源碼
二、OSS所需要的Android權(quán)限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
三、在項(xiàng)目的proguard-rules.pro文件加入混淆配置
-keep class com.alibaba.sdk.android.oss.** { *; } -dontwarn okio.** -dontwarn org.apache.commons.codec.binary.**
四、重點(diǎn)來(lái)了,上代碼(只有上傳和刪除文件的,需要其他的功能就需要自己去研究了)
import android.content.Context; import android.util.Log; import com.alibaba.sdk.android.oss.ClientConfiguration; import com.alibaba.sdk.android.oss.ClientException; import com.alibaba.sdk.android.oss.OSS; import com.alibaba.sdk.android.oss.OSSClient; import com.alibaba.sdk.android.oss.ServiceException; import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback; import com.alibaba.sdk.android.oss.callback.OSSProgressCallback; import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider; import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider; import com.alibaba.sdk.android.oss.internal.OSSAsyncTask; import com.alibaba.sdk.android.oss.model.DeleteObjectRequest; import com.alibaba.sdk.android.oss.model.DeleteObjectResult; import com.alibaba.sdk.android.oss.model.PutObjectRequest; import com.alibaba.sdk.android.oss.model.PutObjectResult; public class AliyunUploadFile { private AliyunUploadView aliyunUploadView; private OSSCredentialProvider credentialProvider; private ClientConfiguration conf; private OSS oss; public AliyunUploadFile(AliyunUploadView aliyunUploadView) { this.aliyunUploadView = aliyunUploadView; } /** * * @param context 上下文 * @param AccessKeyId 存取鍵 一般找你們后端拿 * @param SecretKeyId 密鑰 一般找你們后端拿 * @param SecurityToken 安全符記 一般找你們后端拿 * @param endpoint 端點(diǎn) 一般找你們后端拿 * @param bucketName 桶名 一般找你們后端拿 * @param objectName 文件名 * @param uploadFilePath 文件路徑 */ public void UploadFile(Context context, String AccessKeyId, String SecretKeyId, String SecurityToken , String endpoint, String bucketName, String objectName, String uploadFilePath) { credentialProvider = new OSSStsTokenCredentialProvider(AccessKeyId, SecretKeyId, SecurityToken); conf = new ClientConfiguration(); conf.setConnectionTimeout(15 * 1000); // 連接超時(shí),默認(rèn)15秒 conf.setSocketTimeout(15 * 1000); // socket超時(shí),默認(rèn)15秒 conf.setMaxConcurrentRequest(5); // 最大并發(fā)請(qǐng)求數(shù),默認(rèn)5個(gè) conf.setMaxErrorRetry(2); // 失敗后最大重試次數(shù),默認(rèn)2次 oss = new OSSClient(context, endpoint, credentialProvider, conf); // 構(gòu)造上傳請(qǐng)求。 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, uploadFilePath); // 異步上傳時(shí)可以設(shè)置進(jìn)度回調(diào)。 putObjectRequest.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() { @Override public void onProgress(PutObjectRequest request, long currentSize, long totalSize) { } }); OSSAsyncTask ossAsyncTask = oss.asyncPutObject(putObjectRequest, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() { @Override public void onSuccess(PutObjectRequest request, PutObjectResult result) { aliyunUploadView.UploadSuccess(oss.presignPublicObjectURL(bucketName,objectName)); } @Override public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) { // 請(qǐng)求異常。 if (clientExcepion != null) { // 本地異常,如網(wǎng)絡(luò)異常等。 clientExcepion.printStackTrace(); Log.e("123",clientExcepion+""); aliyunUploadView.Uploaddefeated("網(wǎng)絡(luò)異常"); } if (serviceException != null) { // 服務(wù)異常。 Log.e("123",serviceException+""); aliyunUploadView.Uploaddefeated("服務(wù)異常"); } } }); // ossAsyncTask.cancel(); // 可以取消任務(wù) // ossAsyncTask.waitUntilFinished(); // 等待任務(wù)完成 } public void DeleteFile(Context context, String AccessKeyId, String SecretKeyId, String SecurityToken , String endpoint, String bucketName, String objectName) { credentialProvider = new OSSStsTokenCredentialProvider(AccessKeyId, SecretKeyId, SecurityToken); conf = new ClientConfiguration(); conf.setConnectionTimeout(15 * 1000); // 連接超時(shí),默認(rèn)15秒 conf.setSocketTimeout(15 * 1000); // socket超時(shí),默認(rèn)15秒 conf.setMaxConcurrentRequest(5); // 最大并發(fā)請(qǐng)求數(shù),默認(rèn)5個(gè) conf.setMaxErrorRetry(2); // 失敗后最大重試次數(shù),默認(rèn)2次 oss = new OSSClient(context, endpoint, credentialProvider, conf); // 創(chuàng)建刪除請(qǐng)求。 DeleteObjectRequest delete = new DeleteObjectRequest(bucketName, objectName); // 異步刪除。 OSSAsyncTask deleteTask = oss.asyncDeleteObject(delete, new OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult>() { @Override public void onSuccess(DeleteObjectRequest request, DeleteObjectResult result) { } @Override public void onFailure(DeleteObjectRequest request, ClientException clientExcepion, ServiceException serviceException) { // 請(qǐng)求異常。 if (clientExcepion != null) { // 本地異常,如網(wǎng)絡(luò)異常等。 clientExcepion.printStackTrace(); } if (serviceException != null) { // 服務(wù)異常。 } } }); // deleteTask.cancel(); // 可以取消任務(wù) // deleteTask.waitUntilFinished(); // 等待任務(wù)完成 } public interface AliyunUploadView { void UploadSuccess(String url); void Uploaddefeated(String error); } }
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)Android中阿里云OSS文件上傳的方法大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。