我安装的华为云sdk版本:
implementation 'com.huaweicloud:esdk-obs-android:3.21.12'
调用代码块:
final ObsTask obsTask = new ObsTask(MainActivity.this);
new Thread(new Runnable(){
@Override
public void run() {
obsTask.putObject("pic.jpg","/storage/emulated/0/Android/data/com.tencent.yolov8ncnn/files/pic.jpg");
}
}).start();
ObsTask类:
package com.tencent.yolov8ncnn.huaweiyun;
import android.content.Context;
import android.util.Log;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.tencent.yolov8ncnn.R;
import java.io.File;
import java.io.IOException;
/**
* 华为云对象存储
*/
public class ObsTask{
private final String endPoint;
private final String ak;
private final String sk;
private String bucketName;
private ObsClient obsClient;
public ObsTask(Context context) {
this.endPoint = context.getString(R.string.hwyEndPoint);
this.ak = context.getString(R.string.hwyAk);
this.sk = context.getString(R.string.hwySk);
this.bucketName = context.getString(R.string.hwyBucketName);
this.obsClient = new ObsClient(ak, sk, endPoint);
}
/**
* 华为云上传文件
* @param objectKey
* @param path
* @return
*/
public String putObject(String objectKey, String path) {
File file = new File(path);
if (!file.exists()){
Log.d("ning","文件不存在:"+path);
}
try {
Log.d("ning","bucketName:" + bucketName);
Log.d("ning","objectKey:" + objectKey);
Log.d("ning","fileName:" + file.getName());
obsClient.putObject(bucketName, objectKey, file);
Log.d("ning","上传文件:" + objectKey + " 成功");
return "上传文件:" + objectKey + " 成功";
} catch (ObsException e) {
Log.d("ning","上传文件:" + e.getResponseCode());
Log.d("ning","上传文件:" + e.getErrorMessage());
Log.d("ning","上传文件:" + e.getErrorRequestId());
Log.d("ning","上传文件:" + e.getErrorHostId());
return e.getErrorMessage();
} catch (Exception e) {
return e.getMessage();
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
}
}
}
}
}
报错:FATAL EXCEPTION: Thread-2 Process: com.tencent.yolov8ncnn, PID: 12871
解决:
在app/build.gradle下添加
android {
.........
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}