Skip to content

Commit

Permalink
集成资源上传
Browse files Browse the repository at this point in the history
  • Loading branch information
cleven1 committed Dec 23, 2018
1 parent c1b6560 commit d10d2b3
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 4 deletions.
1 change: 0 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LQRAudioRecord_library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion "27.0.1"
buildToolsVersion '28.0.3'

defaultConfig {
minSdkVersion 15
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ dependencies {
implementation project(':LQRAudioRecord_library')
implementation project(':LQRImagePicker_library')
implementation project(':XhsEmoticonsKeyboard_library')
implementation files('libs/qingstor-sdk-java-2.2.14.jar')
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/cleven/clchat/app/CLApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.cleven.clchat.manager.CLUploadManager;
import com.lqr.emoji.IImageLoader;
import com.lqr.emoji.LQREmotionKit;
import com.lqr.imagepicker.ImagePicker;
Expand Down Expand Up @@ -51,6 +52,8 @@ public void onCreate() {

initOkGo();

CLUploadManager.getInstance().init();

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.cleven.clchat.home.CLEmojiCommon.widget.CLKeyBoardMoreGridView;
import com.cleven.clchat.home.adapter.CLSessionRecyclerAdapter;
import com.cleven.clchat.manager.CLMessageManager;
import com.cleven.clchat.manager.CLUploadManager;
import com.cleven.clchat.utils.CLAPPConst;
import com.lqr.audio.AudioPlayManager;
import com.lqr.audio.AudioRecordManager;
Expand Down Expand Up @@ -451,6 +452,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("CSDN_LQR", imageItem.path);
localMediaPath = imageItem.path;
messageType = CLMessageBodyType.MessageBodyType_Image;
CLUploadManager.getInstance().uploadImage(localMediaPath);
sendMessage("");
}
}
Expand Down
156 changes: 156 additions & 0 deletions app/src/main/java/com/cleven/clchat/manager/CLUploadManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.cleven.clchat.manager;

import android.os.Looper;
import android.widget.Toast;

import com.cleven.clchat.app.CLApplication;
import com.cleven.clchat.utils.CLAPPConst;
import com.cleven.clchat.utils.CLFileUtils;
import com.cleven.clchat.utils.CLUtils;
import com.qingstor.sdk.config.EnvContext;
import com.qingstor.sdk.constants.QSConstant;
import com.qingstor.sdk.exception.QSException;
import com.qingstor.sdk.model.OutputModel;
import com.qingstor.sdk.request.BodyProgressListener;
import com.qingstor.sdk.request.CancellationHandler;
import com.qingstor.sdk.service.Bucket;
import com.qingstor.sdk.upload.UploadManager;
import com.qingstor.sdk.upload.UploadManagerCallback;
import com.qingstor.sdk.upload.impl.FileRecorder;

import java.io.File;
import java.io.IOException;

import dev.utils.LogPrintUtils;
import dev.utils.app.assist.manager.ThreadManager;

/**
* Created by cleven on 2018/12/23.
*/

public class CLUploadManager {
private static CLUploadManager instance = new CLUploadManager();
private Bucket bucket;
private boolean isCancelled;
private UploadManager manager;

private CLUploadManager(){}
public static CLUploadManager getInstance(){
return instance;
}


public void init(){
EnvContext evn = new EnvContext(CLAPPConst.QINGCLOUD_ACCESS_KEY_ID, CLAPPConst.QINGCLOUD_SECRCT_ACCESS_KEY_ID);
bucket = new Bucket(evn,"sh1a","cleven-chat-shanghai");
}

public void uploadAudio(final String filePath){
final File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
LogPrintUtils.eTag("青云", "文件不存在");
return;
}
/// 上传音频
upload("audios/",file);
}

public void uploadVideo(final String filePath){
final File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
LogPrintUtils.eTag("青云", "文件不存在");
return;
}
/// 上传视频
upload("videos/",file);
}

public void uploadAvatar(final String filePath){
final File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
LogPrintUtils.eTag("青云", "文件不存在");
return;
}
/// 上传头像
upload("avatars/",file);
}

public void uploadImage(final String filePath){
final File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
LogPrintUtils.eTag("青云", "文件不存在");
return;
}
/// 上传图片
upload("images/",file);
}


private void upload(final String objectKey, final File file){

FileRecorder recorder = null;
try {
recorder = new FileRecorder(CLFileUtils.getDownloadDir());
} catch (IOException e) {
e.printStackTrace();
}

CancellationHandler cancellationHandler = new CancellationHandler() {
@Override
public boolean isCancelled() {
return isCancelled;
}
};

BodyProgressListener listener = new BodyProgressListener() {
@Override
public void onProgress(final long len, final long size) {
int progress = (int) ((len * 100) / size);
LogPrintUtils.eTag("青云","progress =" + progress);
Toast.makeText(CLApplication.mContext,"progress = " + progress,Toast.LENGTH_SHORT).show();
}
};

UploadManagerCallback callback = new UploadManagerCallback() {
final String TAG = "UploadManagerCallback";

@Override
public void onAPIResponse(OutputModel output) throws QSException {
// 上传成功或失败会回调此方法
LogPrintUtils.eTag(TAG, "code = " + output.getCode());
LogPrintUtils.eTag(TAG, "statueCode = " + output.getStatueCode());
LogPrintUtils.eTag(TAG, "message = " + output.getMessage());
LogPrintUtils.eTag(TAG, "request ID = " + output.getRequestId());
LogPrintUtils.eTag(TAG, "url = " + output.getUrl());

if (output.getStatueCode() == 200 || output.getStatueCode() == 201) {
LogPrintUtils.eTag(TAG, "Upload success.");
Toast.makeText(CLApplication.mContext," 上传成功",Toast.LENGTH_SHORT).show();
} else if (output.getStatueCode() == QSConstant.REQUEST_ERROR_CANCELLED) {
LogPrintUtils.eTag(TAG, "Stopped.");
} else {
LogPrintUtils.eTag(TAG, "Error: " + output.getMessage());
Toast.makeText(CLApplication.mContext,"上传失败",Toast.LENGTH_SHORT).show();
}
}
};

manager = new UploadManager(bucket, recorder, listener, cancellationHandler, callback);
ThreadManager.getInstance().addTask(new Runnable() {
@Override
public void run() {
try {
Looper.prepare();
// manager.put(file);
String imageName = CLUtils.timeStamp + ".png";
manager.put(file,objectKey + imageName, imageName, "");
Looper.loop();
} catch (QSException e) {
e.printStackTrace();
}
}
});

}

}
5 changes: 4 additions & 1 deletion app/src/main/java/com/cleven/clchat/utils/CLAPPConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class CLAPPConst {
public static final String LOGOFF = HTTP_SERVER_BASE_URL + "user/logoff";



/**
* 代理服务器ip地址
*/
Expand Down Expand Up @@ -55,6 +54,10 @@ public class CLAPPConst {
/// 断开连接 + userId
public static final String MQTT_DISCONNECT_TOPIC = "disconnect/user/";

// 青云配置
public static final String QINGCLOUD_ACCESS_KEY_ID = "XLEWZHKBQJZBTPXBAWYP";
public static final String QINGCLOUD_SECRCT_ACCESS_KEY_ID = "aSHZ6MgrWyqK7oXxffZmpuXmdHY1avlQ9YFJdAcY";


//语音存放位置
public static final String AUDIO_SAVE_DIR = CLFileUtils.getDir("audio");
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/cleven/clchat/utils/CLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.cleven.clchat.home.Bean.CLMessageBean;
import com.cleven.clchat.home.Bean.CLMessageBodyType;
import com.cleven.clchat.manager.CLUserManager;
import com.cleven.clchat.home.CLEmojiCommon.utils.CLEmojiFileUtils;
import com.cleven.clchat.manager.CLUserManager;

import java.util.Date;
import java.util.TimeZone;

import dev.utils.LogPrintUtils;
import dev.utils.common.DateUtils;
Expand Down Expand Up @@ -62,4 +63,9 @@ public static String checkLocalPath(CLMessageBean message){

}

public static Date toGMT() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
Date date = new Date();
return date;
}
}

0 comments on commit d10d2b3

Please sign in to comment.