CodeSample小助手 2019-12-30
位置:
res/layout/content_main.xml
内容:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/upload"
android:id="@+id/upload" />
函数实现片段:
Button upload = (Button) findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_UPLOAD_IMAGE);
}
}
函数实现片段:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == RESULT_UPLOAD_IMAGE || requestCode == RESULT_PAUSEABLEUPLOAD_IMAGE) && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Log.d("PickPicture", picturePath);
cursor.close();
try {
Bitmap bm = ImageDisplayer.autoResizeFromLocalFile(picturePath);
displayImage(bm);
File file = new File(picturePath);
displayInfo("文件: " + picturePath + "\n大小: " + String.valueOf(file.length()));
}
catch (IOException e) {
e.printStackTrace();
displayInfo(e.toString());
}
//根据操作不同完成普通上传或者断点上传
if (requestCode == RESULT_UPLOAD_IMAGE) {
final EditText editText = (EditText) findViewById(R.id.edit_text);
String objectName = editText.getText().toString();
//调用简单上传接口上传
ossService.asyncPutImage(objectName, picturePath, getPutCallback(), new ProgressCallbackFactory<PutObjectRequest>().get());
}
}
}
这里省略了对上传结果的处理,可以参考源码中的onSuccess和onFailure的处理。
位置:
res/layout/content_main.xml
内容:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/multipart_upload"
android:id="@+id/multipart_upload" />
函数实现片段:
Button multipart_upload = (Button) findViewById(R.id.multipart_upload);
multipart_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//为了简单化,这里只会同时运行一个断点上传的任务
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_PAUSEABLEUPLOAD_IMAGE);
}
}
);
函数实现片段:
//点击上传:
//这里调用SDK的分片上传接口来上传
task = ossService.asyncMultiPartUpload(objectName, picturePath,getMultiPartCallback().addCallback(new Runnable() {
@Override
public void run() {
pauseTaskStatus = TASK_NONE;
multipart_resume.setEnabled(false);
multipart_pause.setEnabled(false);
task = null;
}
}), new ProgressCallbackFactory<PauseableUploadRequest>().get());
//底层对SDK的封装逻辑,可以看到是在multiPartUploadManager中的asyncUpload实现的断点续传上传
//断点上传,返回的task可以用来暂停任务
public PauseableUploadTask asyncMultiPartUpload(String object,
String localFile,
final OSSCompletedCallback<PauseableUploadRequest,PauseableUploadResult> userCallback,
final OSSProgressCallback<PauseableUploadRequest>userProgressCallback) {
if(object.equals("")) {
Log.w("AsyncMultiPartUpload", "ObjectNull");
return null;
}
File file = new File(localFile);
if (!file.exists()) {
Log.w("AsyncMultiPartUpload", "FileNotExist");
Log.w("LocalFile", localFile);
return null;
}
Log.d("MultiPartUpload", localFile);
PauseableUploadTask task =
multiPartUploadManager.asyncUpload(object, localFile, userCallback,userProgressCallback);
return task;
}