Search in sources :

Example 1 with StorageReference

use of com.google.firebase.storage.StorageReference in project Tapad by berict.

the class PresetStoreAdapter method onFirebasePresetUpdated.

private void onFirebasePresetUpdated(final String presetName, final Runnable onUpdated) {
    StorageReference metadataReference = FirebaseStorage.getInstance().getReferenceFromUrl("gs://tapad-4d342.appspot.com/presets/" + presetName).child("preset.zip");
    metadataReference.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {

        @Override
        public void onSuccess(StorageMetadata storageMetadata) {
            Log.d(TAG, "Successful getting metadata");
            if (storageMetadata.getUpdatedTimeMillis() > new File(PROJECT_LOCATION_PRESETS + "/" + presetName + "/about/json.txt").lastModified()) {
                // firebase preset is updated since last download
                // get the new updated preset
                Log.d(TAG, "Preset updated");
                onUpdated.run();
            } else {
                Log.d(TAG, "Preset not updated");
            }
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d(TAG, "Failed to get preset metadata");
        }
    });
}
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 2 with StorageReference

use of com.google.firebase.storage.StorageReference in project Tapad by berict.

the class FirebaseHelper method downloadFirebase.

public void downloadFirebase(String firebaseLocation, String fileLocation, Activity activity) {
    FirebaseApp.initializeApp(activity);
    StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(FIREBASE_LOCATION + "/" + firebaseLocation);
    this.saveFromFirebase(storageReference, PROJECT_LOCATION + "/" + fileLocation, activity);
}
Also used : StorageReference(com.google.firebase.storage.StorageReference)

Example 3 with StorageReference

use of com.google.firebase.storage.StorageReference 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 4 with StorageReference

use of com.google.firebase.storage.StorageReference in project MadMax by deviz92.

the class FirebaseUtils method addExpenseFirebase.

public String addExpenseFirebase(final Expense expense, ImageView expensePhoto, ImageView billPhoto) {
    Log.d(TAG, "addExpenseFirebase");
    //Aggiungo spesa a Firebase
    final String eID = databaseReference.child("expenses").push().getKey();
    databaseReference.child("expenses").child(eID).setValue(expense);
    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
    databaseReference.child("expenses").child(eID).child("timestamp").setValue(timeStamp);
    //databaseReference.child("expenses").child(eID).child("deleted").setValue(false);
    Bitmap bitmap;
    ByteArrayOutputStream baos;
    byte[] data;
    UploadTask uploadTask;
    // Get the data from an ImageView as bytes
    if (expensePhoto != null) {
        StorageReference uExpensePhotoFilenameRef = storageReference.child("expenses").child(eID).child(eID + "_expensePhoto.jpg");
        expensePhoto.setDrawingCacheEnabled(true);
        expensePhoto.buildDrawingCache();
        bitmap = expensePhoto.getDrawingCache();
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();
        uploadTask = uExpensePhotoFilenameRef.putBytes(data);
        uploadTask.addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                // todo Handle unsuccessful uploads
                Log.e(TAG, "image upload failed");
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                databaseReference.child("expenses").child(eID).child("expensePhoto").setValue(taskSnapshot.getMetadata().getDownloadUrl().toString());
            }
        });
    } else if (expense.getExpensePhoto() != null) {
        databaseReference.child("expenses").child(eID).child("expensePhoto").setValue(expense.getExpensePhoto());
    }
    if (billPhoto != null) {
        StorageReference uBillPhotoFilenameRef = storageReference.child("expenses").child(eID).child(eID + "_billPhoto.jpg");
        // Get the data from an ImageView as bytes
        billPhoto.setDrawingCacheEnabled(true);
        billPhoto.buildDrawingCache();
        bitmap = billPhoto.getDrawingCache();
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();
        uploadTask = uBillPhotoFilenameRef.putBytes(data);
        uploadTask.addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                // todo Handle unsuccessful uploads
                Log.e(TAG, "image upload failed");
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                databaseReference.child("expenses").child(eID).child("billPhoto").setValue(taskSnapshot.getMetadata().getDownloadUrl().toString());
            }
        });
    } else if (expense.getBillPhoto() != null) {
        databaseReference.child("expenses").child(eID).child("billPhoto").setValue(expense.getBillPhoto());
    }
    Log.d(TAG, "creator expense " + expense.getCreatorID());
    //e aggiungo spesa alla lista spese di ogni participant
    for (Map.Entry<String, Double> participant : expense.getParticipants().entrySet()) {
        Log.d(TAG, "partecipant " + participant.getKey());
        //Se il participant corrente รจ il creatore della spesa
        if (participant.getKey().equals(expense.getCreatorID())) {
            //paga tutto lui
            databaseReference.child("expenses").child(eID).child("participants").child(participant.getKey()).child("alreadyPaid").setValue(expense.getAmount());
        } else {
            //gli altri participant inizialmente non pagano niente
            databaseReference.child("expenses").child(eID).child("participants").child(participant.getKey()).child("alreadyPaid").setValue(0);
        }
        //risetto fraction di spesa che deve pagare l'utente, visto che prima si sputtana
        databaseReference.child("expenses").child(eID).child("participants").child(participant.getKey()).child("fraction").setValue(expense.getParticipants().get(participant.getKey()));
        //Aggiungo spesaID a elenco spese dello user
        //todo controllare se utile
        databaseReference.child("users").child(participant.getKey()).child("expenses").child(eID).setValue(true);
    }
    //Aggiungo spesa alla lista spese del gruppo
    databaseReference.child("groups").child(expense.getGroupID()).child("expenses").push();
    databaseReference.child("groups").child(expense.getGroupID()).child("expenses").child(eID).setValue(true);
    return eID;
}
Also used : StorageReference(com.google.firebase.storage.StorageReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 5 with StorageReference

use of com.google.firebase.storage.StorageReference in project quickstart-android by firebase.

the class MyUploadService method uploadFromUri.

// [START upload_from_uri]
private void uploadFromUri(final Uri fileUri) {
    Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
    // [START_EXCLUDE]
    taskStarted();
    showProgressNotification(getString(R.string.progress_uploading), 0, 0);
    // [END_EXCLUDE]
    // [START get_child_ref]
    // Get a reference to store file at photos/<FILENAME>.jpg
    final StorageReference photoRef = mStorageRef.child("photos").child(fileUri.getLastPathSegment());
    // [END get_child_ref]
    // Upload file to Firebase Storage
    Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
    photoRef.putFile(fileUri).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            showProgressNotification(getString(R.string.progress_uploading), taskSnapshot.getBytesTransferred(), taskSnapshot.getTotalByteCount());
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // Upload succeeded
            Log.d(TAG, "uploadFromUri:onSuccess");
            // Get the public download URL
            Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
            // [START_EXCLUDE]
            broadcastUploadFinished(downloadUri, fileUri);
            showUploadFinishedNotification(downloadUri, fileUri);
            taskCompleted();
        // [END_EXCLUDE]
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception exception) {
            // Upload failed
            Log.w(TAG, "uploadFromUri:onFailure", exception);
            // [START_EXCLUDE]
            broadcastUploadFinished(null, fileUri);
            showUploadFinishedNotification(null, fileUri);
            taskCompleted();
        // [END_EXCLUDE]
        }
    });
}
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)

Aggregations

StorageReference (com.google.firebase.storage.StorageReference)12 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)8 UploadTask (com.google.firebase.storage.UploadTask)6 Bitmap (android.graphics.Bitmap)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 NonNull (android.support.annotation.NonNull)4 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)4 File (java.io.File)4 HashMap (java.util.HashMap)3 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 StorageMetadata (com.google.firebase.storage.StorageMetadata)2 User (com.polito.mad17.madmax.entities.User)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 MotionEvent (android.view.MotionEvent)1 FirebaseMetadata (com.bedrock.padder.model.FirebaseMetadata)1