Android upload pictures to seven cattle cloud

Written on the front

  • The time now: 2019-06-23
  • How do I get a seven cattle cloud storage space do not write, focusing on how to upload pictures.

Preparations - processing authority

Network Permissions

  • Adding network access rights
    <uses-permission android:name="android.permission.INTERNET"/>
    
  • Set allowed to send http request, or the default android not allowed to send http request: In manifest file applicationis added on the labelandroid:usesCleartextTraffic="true"

Read the file permissions

  • Add read permissions to the file system
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
  • Because it is dangerous authority, but also for dynamic applications.
    private void getPer()
    {
        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    
        if (permission != PackageManager.PERMISSION_GRANTED)
        {
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }
    

Preparations - Get something related to cloud seven cattle

Obtain AK and SK

To get uploaded to the space (bucket)

Add dependent

implementation 'com.qiniu:qiniu-android-sdk:7.3.15'
implementation 'com.qiniu:qiniu-java-sdk:7.2.+'

Rely Home: https://developer.qiniu.com/kodo/sdk/1236/android      https://github.com/qiniu/java-sdk

upload image

protected void uploadPic()
{
    //指定zone的具体区域
    //FixedZone.zone0   华东机房
    //FixedZone.zone1   华北机房
    //FixedZone.zone2   华南机房
    //FixedZone.zoneNa0 北美机房


    /*
    Configuration config = new Configuration.Builder()
            .chunkSize(512 * 1024)        // 分片上传时,每片的大小。 默认256K
            .putThreshhold(1024 * 1024)   // 启用分片上传阀值。默认512K
            .connectTimeout(10)           // 链接超时。默认10秒
            .useHttps(true)               // 是否使用https 默认是false
            .responseTimeout(60)          // 服务器响应超时。默认60秒
            .recorder(recorder)           // recorder分片上传时,已上传片记录器。默认null
            .recorder(recorder, keyGen)   // keyGen 分片上传时,生成标识符,用于片记录器区分是那个文件的上传记录
            .zone(FixedZone.zone0)        // 设置区域,指定不同区域的上传域名、备用域名、备用IP。
    */


    Configuration config = new Configuration.Builder()
            .useHttps(true)               // 是否使用https上传域名
            .zone(FixedZone.zone0)        // 设置区域,指定不同区域的上传域名、备用域名、备用IP。
            .build();

    UploadManager uploadManager = new UploadManager(config); // UploadManager对象只需要创建一次重复使用

    String data = "/storage/emulated/0/DCIM/Camera/IMG_20190623_172115.jpg"; //要上传的文件
    String key = "test.jpg"; //在服务器的文件名
    /**
    * 生成token
    * create()方法的两个参数分别是 AK SK
    * uoloadToken()方法的参数是 要上传到的空间(bucket)
    */
    String token = Auth.create("kzdlPdSfFXp_Jgj32wlk5ef4r_Y171smSEyzYpX1", "SQ7MwgAQbsT1Sp-RwFg5iqK3m8vUU4fhCgvuEkIb").uploadToken("liyanxing-pic-server");

    /**
    * 调用put方法上传
    * 第一个参数 data:可以是字符串,是要上传图片的路径
    *                可以是File对象,是要上传的文件
    *                可以是byte[]数组,要是上传的数据
    * 第二个参数 key:字符串,是图片在服务器上的名称,要具有唯一性,可以用UUID
    * 第三个参数 token:根据开发者的 AK和SK 生成的token
    * 第四个参数:UpCompletionHandler的实例,有个回调方法
    * 第五个参数:可先参数
    */
    uploadManager.put
    (
        data, key, token,
        new UpCompletionHandler()
        {
            /**
            * 回调方法
            * @param key 开发者设置的 key 也就是文件名
            * @param info 日志,包含上传的ip等
            * @param res 包括一个hash值和key
            */
            @Override
            public void complete(String key, ResponseInfo info, JSONObject res)
            {
                if(info.isOK())
                {
                    Log.i("上传结果:", "Upload Success");
                }
                else
                {
                    Log.i("上传结果:", "Upload Fail");
                    //如果失败,这里可以把info信息上报自己的服务器,便于后面分析上传错误原因
                }
                Log.i("key:", key + "\ninfo:" + info + "\nres:" + res);
            }
        },
        null
    );
}

Guess you like

Origin blog.csdn.net/Sacredness/article/details/93397592