简体   繁体   中英

Uploading local .csv to Firebase Storage

I'm creating an app for schoolproject that reads data from firebase database, converts it to a .csv file and then I want to upload this file to firebase storage so that the user then can share it with just the downloadUrl.

Below is the class for creating a csvfile and then upload it to firebase storage. see csvUploader.

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.CancellableTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageMetadata;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class CsvHandler {

private MainActivity mainActivity;

public CsvHandler(MainActivity mainActivity) {
    this.mainActivity = mainActivity;
}

/**
 * Method that writes a two-dimensional array with strings, to a .csv-file with a specified
 * date as the filename.
 *
 * @param dataArray array to write to a .csv
 * @param callDate  specified date that gets passed to the filename
 */
public void writeFileFromArray(String callDate, String[][] dataArray) {
    String filename = callDate + ".csv";
    //Creates the String which will make up the text for the .csv
    String csvText = "";
    //Adds all elements in Array to the string
    //TODO: Make sure this parses the text correctly to .csv-file format (dependent on Sara & Annies method)
    for (int i = 0; i < dataArray.length; i++) {
        for (int j = 0; j < dataArray[0].length; j++) {
            csvText = csvText + dataArray[i][j];
        }
    }

    //Creates a FileOutputStream for writing the file to internal storage
    FileOutputStream outputStream;
    try {
        //Opens a FileOutputStream to a file with the specified filename.
        //Creates file if it doesn't exist.
        outputStream = mainActivity.openFileOutput(filename, Context.MODE_PRIVATE);
        //Writes the string to the specified file
        outputStream.write(csvText.getBytes());
        //Closes the FileOutputStream to produce a file
        outputStream.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(mainActivity, "Internal Error: No such file found", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(mainActivity, "Internal Error: IOException", Toast.LENGTH_SHORT).show();
    }
}

/**
 * TESTMETOD
 * TODO: Ta bort innan merge med master. Låt stå till develop
 */
public void readCsvFile(String callDate) {
    try {
        String Message;
        FileInputStream fileInputStream = mainActivity.openFileInput(callDate + ".csv");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        while ((Message = bufferedReader.readLine()) != null) {
            stringBuffer.append(Message + "\n");
        }
        Toast.makeText(mainActivity, stringBuffer.toString(), Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Method to extract a filePath for a specified date.
 *
 * @param callDate a String with the date to return a filepath for
 * @return the filepath for the specified date
 */
public String getFilePath(String callDate) {
    String filePath = mainActivity.getFilesDir().getAbsolutePath() + callDate + ".csv";
    Log.e("LOG", "Output from getFilePath " + filePath);
    return filePath;
}


public void csvUploader(String filePath, final String callDate) {
    StorageReference mStorageReference = FirebaseStorage.getInstance().getReference();
    Log.e("LOG", "Entering CSVUPLOADER");
    Uri file = Uri.fromFile(new File(filePath));
    Log.e("csvUploader Uri File:", filePath.toString());

    // Create the file metadata
    StorageMetadata metadata = new StorageMetadata.Builder().setContentType("text/csv").build();
    Log.e("LOG","Metadata: " + metadata.toString());

    // Upload file and metadata to the path 'reports/date.csv'
    CancellableTask uploadTask = mStorageReference.child("reports/" + file.getLastPathSegment()).putFile(file, metadata);


    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
            //System.out.println("Upload is " + progress + "% done");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            Log.e("LOG", "Unsucessfull in CSVUPLOADER");
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // Handle successful uploads on complete
            //Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
            //mainActivity.setDownloadLink(downloadUrl);
            Log.e("LOG", "Successfull in CSVUPLOADER");
            mainActivity.getUrlAsync(callDate);
        }
    });
}

}

Most of this are the example code from firebase.google.com But i don't get it to work.

Logs: E/LOG: Output from getFilePath: /data/user/0/com.example.eliasvensson.busify/files2016-05-18.csv E/LOG: Entering CSVUPLOADER E/LOG: Metadata: com.google.firebase.storage.StorageMetadata@7fd93a9 E/LOG: Unsucessfull in CSVUPLOADER

Whats wrong? I gather that I "reserve" the path on my storage bucket, and then place the file in that place. Is that correct?

Any help would be appreciated.

It all actually almost worked.

the filePath was wrong, outputing /data/user/0/com.example.eliasvensson.busify/files2016-05-18.csv when expecting /data/user/0/com.example.eliasvensson.busify/files/2016-05-18.csv (notice the slash between files and date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM