Search in sources :

Example 6 with OnSuccessListener

use of com.google.android.gms.tasks.OnSuccessListener in project Tapad by berict.

the class FirebaseHelper method saveFirebaseMetadata.

public FirebaseMetadata saveFirebaseMetadata(StorageReference storageReference, final String fileLocation, Activity activity) {
    // permission check
    boolean hasPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    if (!hasPermission) {
        // no permission
        Log.e(TAG, "No permission acquired");
        ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_STORAGE);
    } else {
        if (isConnected(activity)) {
            File file = new File(fileLocation);
            storageReference.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {

                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Log.d(TAG, "Successful download at " + fileLocation);
                }
            }).addOnFailureListener(new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "Failed to download");
                }
            });
        } else {
            Log.e(TAG, "Not connected to the internet");
        }
    }
    return getFirebaseMetadata(activity);
}
Also used : FileDownloadTask(com.google.firebase.storage.FileDownloadTask) File(java.io.File) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 7 with OnSuccessListener

use of com.google.android.gms.tasks.OnSuccessListener in project Tapad by berict.

the class PresetStoreActivity method downloadMetadata.

private void downloadMetadata() {
    // loading start
    setLoadingFinished(false);
    Log.d(TAG, "downloadMetaData");
    boolean hasPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    if (!hasPermission) {
        ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_STORAGE);
    }
    // Make sdcard/Tapad folder
    File folder = new File(tapadFolderPath);
    if (folder.mkdirs()) {
        Log.i(TAG, "folder successfully created");
    } else {
        // folder exists
        Log.e(TAG, "folder already exists");
    }
    // Make sdcard/Tapad/presets folder
    File presets = new File(tapadFolderPath + "/presets");
    if (presets.mkdirs()) {
        Log.i(TAG, "folder successfully created");
    } else {
        // folder exists
        Log.e(TAG, "folder already exists");
    }
    final File metadata = new File(tapadFolderPath + "/presets/metadata.txt");
    StorageReference metadataReference = FirebaseStorage.getInstance().getReferenceFromUrl("gs://tapad-4d342.appspot.com/presets").child("metadata.txt");
    metadataReference.getFile(metadata).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {

        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Log.d(TAG, "Successful download at " + metadata.toString());
            setAdapter();
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "Failed to download");
        }
    });
}
Also used : FileDownloadTask(com.google.firebase.storage.FileDownloadTask) StorageReference(com.google.firebase.storage.StorageReference) File(java.io.File) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 8 with OnSuccessListener

use of com.google.android.gms.tasks.OnSuccessListener in project duckFood by sanjaytharagesh31.

the class Main2Activity method uploadFile.

// this method is uploading the file
// the code is same as the previous tutorial
// so we are not explaining it
private void uploadFile(Uri data, int r) {
    if (r == PICK_PDF_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference sRef = mStorageReference.child("/PDF Uploads/" + editTextFilename.getText().toString() + ".pdf");
        sRef.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("PDF File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                mDatabaseReference.child(mDatabaseReference.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    } else if (r == PICK_VIDEO_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference mp4ref = videoStorageRefernce.child("/MP4 Uploads/" + editTextFilename.getText().toString() + ".mp4");
        mp4ref.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("Video File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                videoDatabaseRefernce.child(videoDatabaseRefernce.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            // Important Code to be added
            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    } else if (r == PICK_TEXT_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference txtref = textStorageReference.child("/Text Uploads/" + editTextFilename.getText().toString() + ".txt");
        txtref.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("Text File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                textDatabaseReference.child(textDatabaseReference.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            // Important Code to be added
            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    } else if (r == PICK_AUDIO_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference audioref = textStorageReference.child("/Audio Uploads/" + editTextFilename.getText().toString() + ".txt");
        audioref.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("Audio File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                audioDatabaseReference.child(audioDatabaseReference.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            // Important Code to be added
            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    }
}
Also used : StorageReference(com.google.firebase.storage.StorageReference) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) OnProgressListener(com.google.firebase.storage.OnProgressListener) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 9 with OnSuccessListener

use of com.google.android.gms.tasks.OnSuccessListener in project OnlineCanteen by josephgunawan97.

the class RegisterProductActivity method uploadImage.

// To upload image
private void uploadImage() {
    Log.d(TAG, "Uploading...");
    final StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("product/" + System.currentTimeMillis() + ".jpg");
    if (imageUri != null) {
        profileImageRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();
                profPicUrl = downloadUrl.toString();
                Product productInfo = new Product(user.getUid(), productnameET.getText().toString(), Integer.parseInt(quantityET.getText().toString()), Integer.parseInt(priceET.getText().toString()), profPicUrl);
                databaseProducts.push().setValue(productInfo);
                // profPicUrl = profileImageRef.toString();
                // Toast.makeText(getApplicationContext(),profPicUrl,Toast.LENGTH_LONG).show();
                Log.d(TAG, "Success in uploading");
            // backToScreen();
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Image failed to upload", Toast.LENGTH_LONG).show();
            // backToScreen();
            }
        });
    }
}
Also used : UploadTask(com.google.firebase.storage.UploadTask) StorageReference(com.google.firebase.storage.StorageReference) Product(com.example.asus.onlinecanteen.model.Product) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) Uri(android.net.Uri) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 10 with OnSuccessListener

use of com.google.android.gms.tasks.OnSuccessListener in project kijenzi-mobile by kijenzi.

the class FirebaseFiles method getFile.

public File getFile(String path, String suffix) {
    File file = null;
    StorageReference model = ref.child(path);
    try {
        file = File.createTempFile("design-", suffix);
        model.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                System.out.println(taskSnapshot.getTotalByteCount() + " HERE IS THE TOTAL BYTE COUNT");
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                System.out.println("FAILED");
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
        file = null;
        System.out.println("FAILED");
    }
    return file;
}
Also used : FileDownloadTask(com.google.firebase.storage.FileDownloadTask) StorageReference(com.google.firebase.storage.StorageReference) IOException(java.io.IOException) File(java.io.File) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) IOException(java.io.IOException)

Aggregations

OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)44 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)43 StorageReference (com.google.firebase.storage.StorageReference)13 UploadTask (com.google.firebase.storage.UploadTask)10 File (java.io.File)9 AuthResult (com.google.firebase.auth.AuthResult)8 Uri (android.net.Uri)7 ApiException (com.google.android.gms.common.api.ApiException)6 FileDownloadTask (com.google.firebase.storage.FileDownloadTask)6 IOException (java.io.IOException)6 ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)5 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)4 InputImage (com.google.mlkit.vision.common.InputImage)4 NonNull (android.support.annotation.NonNull)3 View (android.view.View)3 WritableArray (com.facebook.react.bridge.WritableArray)3 TaskFailureLogger (com.firebase.ui.auth.ui.TaskFailureLogger)3 FirebaseUser (com.google.firebase.auth.FirebaseUser)3 OnProgressListener (com.google.firebase.storage.OnProgressListener)3 StorageMetadata (com.google.firebase.storage.StorageMetadata)3