详细解答可以参考官方帮助文档
简单上传就是调用OSS API中的Put Object接口,一次性将选择的文件上传到OSS上。
位置:
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的处理。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。