-2

i have a working Uploadfile() process, but im wanting to change it to run on a thread instead of main ui thread. i dont fully understand the types you have to pass i.e params, progress, result my working code:



private void Uploadfile(){
            if (imageUri != null){

StorageReference fileReference = StorageRef.child(System.currentTimeMillis() + "." + getFileExtension(imageUri));
                uploadtask = fileReference.putFile(imageUri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                Toast.makeText(gallery.this, "Upload successful", Toast.LENGTH_SHORT).show();
                                Upload upload= new Upload(Filename.getText().toString().trim(),
                                        taskSnapshot.getDownloadUrl().toString());
                                //create new database entry with unique image id
                                String uploadId = DBREF.push().getKey();
                                DBREF.child(uploadId).setValue(upload);
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(gallery.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                    }
                });
            }
            else {
                Toast.makeText(this, "no file selected",Toast.LENGTH_SHORT).show();
            }
        }


And the thread i tried to write:


private class Uploadfile extends AsyncTask<URL, Void, Void> { protected Void doInBackground(URL... urls) { if (imageUri != null) { StorageReference fileReference = StorageRef.child(System.currentTimeMillis() + "." + getFileExtension(imageUri)); uploadtask = fileReference.putFile(imageUri) .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { Upload upload = new Upload(Filename.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString()); //create new database entry with unique image id String uploadId = DBREF.push().getKey(); DBREF.child(uploadId).setValue(upload); } }); } return null; } protected void onPostExecute(Void result) { } }

Im not sure what to pass to the onPostExecute() function, any help welcome

  • please paste your code directly. please do not use links or images to display code. Links are not always dependable and reduce the value of your post. – JDOaktown Apr 26 '18 at 17:47

1 Answers1

0

At onPostResult you callback the caller so it know the task has finished.

android asynctask sending callbacks to ui

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167