解决华为云android OBS报错:FATAL EXCEPTION: Thread-2

我安装的华为云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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值