0

我正在开发一个 android 应用程序来将文件上传到亚马逊 s3 服务器。它工作正常。现在,我想在应用程序中添加暂停和恢复功能。我正在使用传输效用函数。是否有任何解决方案或如何通过共享偏好来实现?请帮我。

MainActivity.java

public class MainActivity extends AppCompatActivity {



Button button;
Button pause;
Button resume;

TextView size;
TextView completed;
TextView per;


static String filePath = "";
static String extension = "";
static String fileTempName = "";

float percentage = 0;
Uri selectedFileUri = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    pause = (Button) findViewById(R.id.pause);
    resume = (Button) findViewById(R.id.resume);
    size = (TextView) findViewById(R.id.size);
    completed = (TextView) findViewById(R.id.completed);
    per = (TextView) findViewById(R.id.per);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();

            intent.setType("*/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select File"), 1);


        }
    });


    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "clicked",Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        selectedFileUri = data.getData();
        filePath = new PathUtils().getPath(MainActivity.this, selectedFileUri);
        extension = filePath.substring(filePath.lastIndexOf("."));
        Log.d("LOGTAG", "extension : "+extension);
        fileUpload();
    }
}
private void fileUpload() {
    File file = new File(filePath);
    String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    Random Number = new Random();
    int Rnumber = Number.nextInt(10000000);
    fileTempName = Rnumber  +timeStamp+extension;
    Log.d("LOGTAG", "timestamp" + fileTempName);// Initialize the Amazon Cognito credentials provider
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(),
            "xxxxx:xxx-xxxxxxx-xxx-xxxxxxx", // Identity Pool ID
            Regions.REGION // Region
    );
    AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
    s3.setRegion(com.amazonaws.regions.Region.getRegion(Regions.AP_SOUTH_1));
    try {

        s3.putObject(new PutObjectRequest(
                BUCKET-NAME, fileTempName, file).withCannedAcl(CannedAccessControlList.PublicRead)); // this will set the permission as PublicRead
    } catch (Exception ex) {
        ex.getMessage();
    }
    TransferUtility transferUtility = new TransferUtility(s3, this);
    TransferObserver transferObserver = transferUtility.upload("BUCKET-NAME",fileTempName,file);
    putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all
    s3.putObject(putObjectRequest); // upload file*/
    transferObserver.setTransferListener(new TransferListener() {
        @Override
        public void onStateChanged(int id, TransferState state) {
            if(state == TransferState.COMPLETED) {
                Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            /*int percentage = (int) (bytesCurrent / bytesTotal * 100);
            Log.d("LOGTAG", "Percentage : " + percentage);*/
            long _bytesCurrent = bytesCurrent;
            long _bytesTotal = bytesTotal;
            percentage =  ((float)_bytesCurrent /(float)_bytesTotal * 100);
            String pr = String.format("%.2f", percentage);
            per.setText(pr + "%");
            completed.setText(Long.toString(bytesCurrent));
            size.setText(Long.toString(bytesTotal));
            Log.d("percentage","Per1 : " +percentage);
            Log.d("LOGTAG", String.format("onProgressChanged: %d, total: %d, current: %d ",
                    id, bytesTotal, bytesCurrent));
        }

        @Override
        public void onError(int id, Exception ex) {
            Log.e("LOGTAG", "Error : " + ex.getMessage());

        }
    });
}
}
4

2 回答 2

0

我从 https://github.com/awslabs/aws-sdk-android-samples/tree/master/S3TransferUtilitySample得到了答案

在此链接中包含文件上传的暂停和恢复功能的代码。我得到正确的结果。

于 2016-10-31T11:08:17.983 回答
0

如果有人仍在寻找解决方案,您可以使用 TransferUtility 的暂停和恢复方法暂停和恢复传输。这些方法将输入作为您在 TransferListener 类的 onStateChanged 方法中获得的 transferId。

TransferUtility 类 - https://github.com/aws-amplify/aws-sdk-android/blob/main/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility .java

于 2021-11-11T05:44:29.057 回答