import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosException;
import com.volcengine.tos.model.object.GetObjectV2Input;
import com.volcengine.tos.model.object.GetObjectV2Output;
import com.volcengine.tos.model.object.ObjectMetaRequestOptions;
import java.io.FileOutputStream;
import java.io.IOException;
public class GetObjectRangeExample extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
String endpoint = "your endpoint";
String region = "your region";
String accessKey = "your access key";
String secretKey = "your secret key";
String securityToken = "your security token";
String bucketName = "your bucket name";
String objectKey = "your object key";
String filePath = "your file path";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, securityToken);
Thread tosThread = new Thread(new Runnable() {
ObjectMetaRequestOptions options = new ObjectMetaRequestOptions().setRange(rangeStart, rangeEnd);
GetObjectV2Input input = new GetObjectV2Input().setBucket(bucketName).setKey(objectKey).setOptions(options);
try(FileOutputStream fileOutputStream = new FileOutputStream(filePath);
GetObjectV2Output output = tos.getObject(input)) {
byte[] buffer = new byte[1024];
while ((length = output.getContent().read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, length);
Log.i("getObject","getObject succeed, object's metadata is " + output.getGetObjectBasicOutput());
} catch (IOException e) {
Log.e("IOException", "write data to file failed");
} catch (TosException e) {
if (e.getStatusCode() == 404) {
Log.e("TosException", "the object you want to download is not found");
Log.e("TosException", "getObject failed");