您好,登錄后才能下訂單哦!
Android中阿里云OSS文件上傳的方法?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
一、項目導入阿里云oss的SDK
方式1:在build文件添加依賴(博主和官方一致推薦)
dependencies { //阿里云oss implementation 'com.aliyun.dpa:oss-android-sdk:+' }
方式2:源碼編譯jar包 源碼傳送門:官方阿里云oss源碼
二、OSS所需要的Android權限
<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" />
三、在項目的proguard-rules.pro文件加入混淆配置
-keep class com.alibaba.sdk.android.oss.** { *; } -dontwarn okio.** -dontwarn org.apache.commons.codec.binary.**
四、重點來了,上代碼(只有上傳和刪除文件的,需要其他的功能就需要自己去研究了)
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 端點 一般找你們后端拿 * @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); // 連接超時,默認15秒 conf.setSocketTimeout(15 * 1000); // socket超時,默認15秒 conf.setMaxConcurrentRequest(5); // 最大并發請求數,默認5個 conf.setMaxErrorRetry(2); // 失敗后最大重試次數,默認2次 oss = new OSSClient(context, endpoint, credentialProvider, conf); // 構造上傳請求。 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, uploadFilePath); // 異步上傳時可以設置進度回調。 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) { // 請求異常。 if (clientExcepion != null) { // 本地異常,如網絡異常等。 clientExcepion.printStackTrace(); Log.e("123",clientExcepion+""); aliyunUploadView.Uploaddefeated("網絡異常"); } if (serviceException != null) { // 服務異常。 Log.e("123",serviceException+""); aliyunUploadView.Uploaddefeated("服務異常"); } } }); // ossAsyncTask.cancel(); // 可以取消任務 // ossAsyncTask.waitUntilFinished(); // 等待任務完成 } 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); // 連接超時,默認15秒 conf.setSocketTimeout(15 * 1000); // socket超時,默認15秒 conf.setMaxConcurrentRequest(5); // 最大并發請求數,默認5個 conf.setMaxErrorRetry(2); // 失敗后最大重試次數,默認2次 oss = new OSSClient(context, endpoint, credentialProvider, conf); // 創建刪除請求。 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) { // 請求異常。 if (clientExcepion != null) { // 本地異常,如網絡異常等。 clientExcepion.printStackTrace(); } if (serviceException != null) { // 服務異常。 } } }); // deleteTask.cancel(); // 可以取消任務 // deleteTask.waitUntilFinished(); // 等待任務完成 } public interface AliyunUploadView { void UploadSuccess(String url); void Uploaddefeated(String error); } }
感謝各位的閱讀!看完上述內容,你們對Android中阿里云OSS文件上傳的方法大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。