android 阿里oss,Android OSS阿里图片上传

一 、阿里的官方文档

二、Android图片上传的流程 + OSS

选择图片

压缩图片

构建阿里上传请求对象OSS

构建阿里的文件上传对象PutObjectRequest

PutObjectRequest设置上传进度监听

OSS对象发起请求asyncPutObject

前面2步我就不写了,主要是写后面这几步。

三、怎么构建阿里的OSS对象

构建OSS对象要一个OSSCredentialProvider,OSSCredentialProvider这个对象有很多子对象,比例可以带Token啥的。

创建OSSCredentialProvider对象

/**

* 创建OSS对象的OSSCredentialProvider

*/

public class OSSConfig {

// Access Key id 问后台要

public static final String AK = "LTXXXXXXXXQu";

// SecretKeyId 问后台要

public static final String SK = "9eKXXXXXXXXZbkZ";

public static OSSCredentialProvider newCustomSignerCredentialProvider() {

return new OSSCustomSignerCredentialProvider() {

@Override

public String signContent(String content) {

return OSSUtils.sign(AK, SK, content);

}

};

}

}

创建OSS对象,有网络请求,要异步

/**

* 创建OSS对象

*/

public OSS getOSS(Context context) {

if (mOSS == null) {

OSSCredentialProvider provider = OSSConfig.newCustomSignerCredentialProvider();

ClientConfiguration conf = new ClientConfiguration();

conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒

conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒

conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个

conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次

mOSS = new OSSClient(context, endpoint, provider, conf);

}

return mOSS;

}

四、由于上传图片项目经常要用,创建一个管理类来管理图片上传

/**

* OSS图片上传的管理类

*/

public class OssManager {

/**

* 图片上传的地址

* 问后台要的

*/

public static String endpoint = "https://oss-xxxx.com";

/**

* 图片的访问地址的前缀

* 其实就是: bucketName + endpoint

*/

public static String prefix = "https://xxx.oss-xxxx.com/";

/**

* Bucket是OSS上的命名空间

* 问后台要的

*/

public static String bucketName = "xxx";

/**

* 图片保存到OSS服务器的目录,问后台要

*/

public static String dir = "app/";

private OSS mOSS;

private static OssManager mInstance;

public static OssManager getInstance() {

if (mInstance == null) {

synchronized (OssManager.class) {

mInstance = new OssManager();

}

}

return mInstance;

}

/**

* 创建OSS对象

*/

private OSS getOSS(Context context) {

if (mOSS == null) {

OSSCredentialProvider provider = OSSConfig.newCustomSignerCredentialProvider();

ClientConfiguration conf = new ClientConfiguration();

conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒

conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒

conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个

conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次

mOSS = new OSSClient(context, endpoint, provider, conf);

}

return mOSS;

}

/**

* 图片上传

*

* @param context

* @param uploadFilePath 图片的本地路径

* @param onUploadListener 回调监听

*/

public void upload(final Context context, final int position, final String uploadFilePath,

final OnUploadListener onUploadListener) {

Observable.just(context)

.map(new Function() {

@Override

public OSS apply(Context context) throws Exception {

return getOSS(context);

}

})

.map(new Function() {

@Override

public String apply(OSS oss) throws Exception {

// 创建压缩图片的路径

File imageFilePath = FileUtil.createImageFilePath();

// 压缩图片

ImageCompressUtil.compress(context, uploadFilePath, imageFilePath.getAbsolutePath());

// 创建上传的对象

PutObjectRequest put = new PutObjectRequest(bucketName,

dir + getUUIDByRules32Image()

, imageFilePath.getAbsolutePath());

// 上传的进度回调

put.setProgressCallback(new OSSProgressCallback() {

@Override

public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {

if (onUploadListener == null) {

return;

}

onUploadListener.onProgress(position, currentSize, totalSize);

}

});

oss.asyncPutObject(put, new OSSCompletedCallback() {

@Override

public void onSuccess(PutObjectRequest request, PutObjectResult result) {

if (onUploadListener == null) {

return;

}

String imageUrl = request.getObjectKey();

onUploadListener.onSuccess(position, uploadFilePath,

prefix + imageUrl);

}

@Override

public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {

serviceException.printStackTrace();

clientException.printStackTrace();

if (onUploadListener == null) {

return;

}

onUploadListener.onFailure(position);

}

});

return imageFilePath.getAbsolutePath();

}

})

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe();

}

public interface OnUploadListener {

/**

* 上传的进度

*/

void onProgress(int position, long currentSize, long totalSize);

/**

* 成功上传

*/

void onSuccess(int position, String uploadPath, String imageUrl);

/**

* 上传失败

*/

void onFailure(int position);

}

/**

* 上传到后台的图片的名称

*/

public static String getUUIDByRules32Image() {

StringBuffer generateRandStr = null;

try {

String rules = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int rpoint = 0;

generateRandStr = new StringBuffer();

Random rand = new Random();

int length = 32;

for (int i = 0; i < length; i++) {

if (rules != null) {

rpoint = rules.length();

int randNum = rand.nextInt(rpoint);

generateRandStr.append(rules.substring(randNum, randNum + 1));

}

}

} catch (Exception e) {

e.printStackTrace();

}

if (generateRandStr == null) {

return "getUUIDByRules32Image.png";

}

return generateRandStr + ".png";

}

}

大概的流程就是和开始说的一样。就多了自已定义的监听回调,图片上传可能是多图上传。在回调里加了个回调的位置,以便更新UI或者其它的操作。

五、实际使用

private void uploadImage(final List images) {

for (int i = 0; i < images.size(); i++) {

ImageBean bean = images.get(i);

OssManager.getInstance().upload(getAppActivity(), i, bean.path,

new OssManager.OnUploadListener() {

@Override

public void onProgress(int position, long currentSize, long totalSize) {

LogUtils.e("position = " + position + " onProgress = " + currentSize);

}

@Override

public void onSuccess(int position, String uploadPath, String imageUrl) {

LogUtils.e("position = " + position + " imageUrl = " + imageUrl

+"\n uploadPath = "+uploadPath);

}

@Override

public void onFailure(int position) {

LogUtils.e("position = " + position);

}

}

);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用阿里云官方提供的 OSS(对象存储服务)SDK 来实现 Android 上的文件上功能。以下是一个简单的示例代码,需要先引入阿里云 OSS SDK 的依赖: ```java implementation 'com.aliyun.dpa:oss-android-sdk:3.1.8' ``` 然后,在你的代码中使用以下方法来实现文件上: ```java import com.aliyun.dpa.oss.app.AppServiceFactory; import com.aliyun.dpa.oss.app.OSSClientFactory; import com.aliyun.dpa.oss.app.RunningConfig; import com.aliyun.dpa.oss.app.ServiceFactory; import com.aliyun.dpa.oss.app.UploadConfiguration; import com.aliyun.dpa.oss.app.UploadConfigurationFactory; import com.aliyun.dpa.oss.app.UploadManager; import com.aliyun.dpa.oss.app.util.FileUtil; import com.aliyun.dpa.oss.app.util.StringUtil; import com.aliyun.dpa.oss.app.util.UIUtil; import com.aliyun.dpa.oss.callback.ResultCallback; import com.aliyun.dpa.oss.model.OSSUploadConfiguration; import com.aliyun.dpa.oss.task.PutObjectTask; import com.aliyun.dpa.oss.task.TaskCancelledException; public class AliOSSUploader { private static final String ENDPOINT = "your_oss_endpoint"; // 你的 OSS Endpoint private static final String ACCESS_KEY_ID = "your_access_key_id"; // 你的 Access Key ID private static final String ACCESS_KEY_SECRET = "your_access_key_secret"; // 你的 Access Key Secret private static final String BUCKET_NAME = "your_bucket_name"; // 你的 Bucket 名称 public void uploadFile(String filePath) { // 创建 OSS 客户端 RunningConfig runningConfig = new RunningConfig(ACCESS_KEY_ID, ACCESS_KEY_SECRET, ENDPOINT); AppServiceFactory.init(runningConfig); ServiceFactory serviceFactory = OSSClientFactory.createServiceFactory(); UploadConfiguration configuration = UploadConfigurationFactory.getUploadConfiguration(); UploadManager uploadManager = serviceFactory.createUploadManager(configuration); // 构造上任务 PutObjectTask putObjectTask = new PutObjectTask(BUCKET_NAME, StringUtil.generateRandomKey(), filePath); // 设置上结果回调 putObjectTask.setResultCallback(new ResultCallback() { @Override public void onSuccess(Object object) { // 文件上成功 UIUtil.showToast("文件上成功"); } @Override public void onFailure(Exception e) { // 文件上失败 UIUtil.showToast("文件上失败:" + e.getMessage()); } @Override public void onProgress(Object object, long currentSize, long totalSize) { // 文件上进度 int progress = (int) (currentSize * 100 / totalSize); UIUtil.showToast("文件上进度:" + progress + "%"); } @Override public void onCancel(Object object) { // 文件上取消 UIUtil.showToast("文件上取消"); } }); // 开始上任务 try { uploadManager.upload(putObjectTask); } catch (TaskCancelledException e) { // 上任务取消 UIUtil.showToast("文件上取消"); } } } ``` 以上代码中的 `your_oss_endpoint`、`your_access_key_id`、`your_access_key_secret` 和 `your_bucket_name` 分别是你的 OSS Endpoint、Access Key ID、Access Key Secret 和 Bucket 名称。你需要将它们替换为你自己的实际值。 调用 `uploadFile` 方法并入要上的文件路径,即可实现文件上到阿里云 OSS。记得在 AndroidManifest.xml 中添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 希望对你有帮助!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值