upload progress

This commit is contained in:
Page Asgardius 2022-09-13 14:02:21 -07:00
parent 321c31978d
commit a226cc9f70
2 changed files with 25 additions and 14 deletions

View file

@ -5,7 +5,6 @@ import static android.content.ContentValues.TAG;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
@ -31,11 +30,12 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
public class Uploader extends AppCompatActivity { public class Uploader extends AppCompatActivity {
String username, password, endpoint, bucket, prefix, location, fkey; String username, password, endpoint, bucket, prefix, location, fkey;
boolean isfolder; boolean isfolder;
Uri file, folder; Uri fileuri, folder;
EditText fprefix; EditText fprefix;
Region region; Region region;
S3ClientOptions s3ClientOptions; S3ClientOptions s3ClientOptions;
@ -79,7 +79,7 @@ public class Uploader extends AppCompatActivity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
//buttonaction //buttonaction
if (file == null && folder == null) { if (fileuri == null && folder == null) {
Toast.makeText(Uploader.this, getResources().getString(R.string.no_file_selected), Toast.LENGTH_SHORT).show(); Toast.makeText(Uploader.this, getResources().getString(R.string.no_file_selected), Toast.LENGTH_SHORT).show();
} else { } else {
//Toast.makeText(CreateBucket.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); //Toast.makeText(CreateBucket.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show();
@ -97,17 +97,28 @@ public class Uploader extends AppCompatActivity {
try { try {
//Your code goes here //Your code goes here
//s3client.createBucket(bucket, location); //s3client.createBucket(bucket, location);
System.out.println(fkey); //System.out.println(fkey);
ContentResolver cr = getContentResolver(); File ufile = readContentToFile(fileuri);
InputStream is = cr.openInputStream(file); long filesize =ufile.length();
File ufile = readContentToFile(file);
PutObjectResult upload = s3client.putObject(bucket, fkey, ufile); PutObjectResult upload = s3client.putObject(bucket, fkey, ufile);
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
simpleProgressBar.setVisibility(View.VISIBLE);
while (upload.getMetadata().getContentLength() <= filesize-1) {
if(upload.getMetadata().getContentLength() != 0) {
simpleProgressBar.setProgress(((int)filesize / (int)upload.getMetadata().getContentLength())*100);
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
simpleProgressBar.setProgress(100);
// Sending reference and data to Adapter // Sending reference and data to Adapter
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show();
//simpleProgressBar.setVisibility(View.INVISIBLE); //simpleProgressBar.setVisibility(View.INVISIBLE);
} }
}); });
@ -166,16 +177,16 @@ public class Uploader extends AppCompatActivity {
// Instead, a URI to that document will be contained in the return intent // Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter. Pull that uri using "resultData.getData()" // provided to this method as a parameter. Pull that uri using "resultData.getData()"
if (resultData != null && resultData.getData() != null) { if (resultData != null && resultData.getData() != null) {
file = resultData.getData(); fileuri = resultData.getData();
filename = file.getPath().split("/"); filename = fileuri.getPath().split("/");
System.out.println("File selected successfully"); //System.out.println("File selected successfully");
System.out.println("content://com.android.externalstorage.documents"+file.getPath()); //System.out.println("content://com.android.externalstorage.documents"+file.getPath());
} else { } else {
Toast.makeText(Uploader.this, getResources().getString(R.string.file_path_fail), Toast.LENGTH_SHORT).show(); Toast.makeText(Uploader.this, getResources().getString(R.string.file_path_fail), Toast.LENGTH_SHORT).show();
finish(); finish();
} }
} else { } else {
System.out.println("User cancelled file browsing {}"); //System.out.println("User cancelled file browsing {}");
finish(); finish();
} }
} }

View file

@ -48,7 +48,7 @@
android:id="@+id/simpleProgressBar" android:id="@+id/simpleProgressBar"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:indeterminate="true" android:indeterminate="false"
android:max="100" android:max="100"
android:padding="20dp" android:padding="20dp"
android:progress="50" android:progress="50"