Search in sources :

Example 31 with OnSuccessListener

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

the class PresetStoreActivity method isFirebaseMetadataUpdated.

private boolean isFirebaseMetadataUpdated(Context context) {
    isFMUpdated = false;
    if (isConnected(context)) {
        Log.d(TAG, "Connected to the internet");
        StorageReference metadataReference = FirebaseStorage.getInstance().getReferenceFromUrl("gs://tapad-4d342.appspot.com/presets").child("metadata.txt");
        metadataReference.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {

            @Override
            public void onSuccess(StorageMetadata storageMetadata) {
                Log.d(TAG, "Successful getting metadata");
                if (storageMetadata.getUpdatedTimeMillis() > new File(metadataLocation).lastModified()) {
                    // firebase metadata is updated since last download
                    // get the new updated metadata
                    Log.d(TAG, "File updated");
                    isFMUpdated = true;
                } else {
                    Log.d(TAG, "File not updated");
                    isFMUpdated = false;
                }
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, "Failed to get metadata");
                isFMUpdated = false;
            }
        });
        return isFMUpdated;
    } else {
        Log.d(TAG, "Disconnected from the internet");
        return isFMUpdated;
    }
}
Also used : StorageMetadata(com.google.firebase.storage.StorageMetadata) StorageReference(com.google.firebase.storage.StorageReference) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) File(java.io.File) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 32 with OnSuccessListener

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

the class FirebaseHelper method saveFromFirebase.

public void saveFromFirebase(StorageReference storageReference, final String fileLocation, final Runnable onSuccess, final Runnable onFailure, 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);
                    onSuccess.run();
                }
            }).addOnFailureListener(new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "Failed to download");
                    onFailure.run();
                }
            });
        } else {
            Log.e(TAG, "Not connected to the internet");
        }
    }
}
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 33 with OnSuccessListener

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

the class EditProductActivity method uploadImage.

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();
                submitData();
                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();
            }
        });
    } else
        submitDatawithoutImage();
}
Also used : UploadTask(com.google.firebase.storage.UploadTask) StorageReference(com.google.firebase.storage.StorageReference) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) Uri(android.net.Uri) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 34 with OnSuccessListener

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

the class RequestTopUpActivity method uploadImage.

// To upload image
private void uploadImage() {
    final Map topUp = new HashMap();
    database = FirebaseDatabase.getInstance().getReference("topuprequest");
    final StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("topuprequest/" + 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();
                String transferproofUrl = downloadUrl.toString();
                TopUp topUp = new TopUp(transferName.getText().toString(), userID, transferproofUrl.toString(), bankName.getText().toString(), Integer.parseInt(amount.getText().toString()));
                database.push().setValue(topUp);
            }
        }).addOnFailureListener(new OnFailureListener() {

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

Example 35 with OnSuccessListener

use of com.google.android.gms.tasks.OnSuccessListener in project BloodHub by kazijehangir.

the class RequestListDataAdapter method onBindViewHolder.

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    BloodRequest request = requests.get(position);
    String key = keys.get(position);
    holder.cv.setTag(key);
    holder.mName.setText(request.name);
    holder.mNeeds.setText(request.quantity + " bags of " + request.blood_group);
    holder.mLocation.setText(request.location);
    String date = DateFormat.getDateInstance().format(new Date(request.date));
    holder.mWhen.setText(date);
    if (date.equals(DateFormat.getDateInstance().format(new Date()))) {
        holder.mWhen.setText("URGENT");
        holder.mWhen.setTextColor(0xFFFF0000);
    }
    holder.mDiagnosis.setText(request.diagnosis);
    if (request.transport) {
        holder.mTransport.setText("Available");
        holder.mTransportImage.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_car));
    } else {
        holder.mTransport.setText("Not Available");
        holder.mTransportImage.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_no_car));
    }
    try {
        final File localFile = File.createTempFile("images", "jpg");
        FirebaseStorage.getInstance().getReference().child("bloodrequests").child(key).getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                // Successfully downloaded data to local file
                holder.mImage.setImageDrawable(Drawable.createFromPath(localFile.getPath()));
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                holder.mImage.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.girl));
            // Toast.makeText(mContext,"Error loading image: " + exception.toString(),
            // Toast.LENGTH_LONG).show();
            }
        });
    } catch (Exception e) {
    // IOException: error making temp image
    // Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG);
    }
}
Also used : FileDownloadTask(com.google.firebase.storage.FileDownloadTask) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest) File(java.io.File) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) Date(java.util.Date) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

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