How to upload an audio file to Parse.com - Android

I have a file path that I want to upload to Parse.com.

For example, one of the file paths: "/storage/emulated/0/app100/2015-06-04_00:45:16_RecordSound.3gp"

Now I want to upload it to Parse.com. Any solution how to do this?

I tried to write a method for this, but it doesn't work:

private ParseObject uploadAudioToParse(File audioFile, ParseObject po, String columnName){

    if(audioFile != null){
        Log.d("EB", "audioFile is not NULL: " + audioFile.toString());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(audioFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int read;
        byte[] buff = new byte[1024];
        try {
            while ((read = in.read(buff)) > 0)
            {
                out.write(buff, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] audioBytes = out.toByteArray();
        // Create the ParseFile
        ParseFile file = new ParseFile(audioFile.getName() , audioBytes);
        // Upload the file into Parse Cloud
        file.saveInBackground();
        po.put(columnName, file);
    }
    return po;
}

      

Thank!

+3


source to share


1 answer


Try the following:

private ParseObject uploadAudioToParse(File audioFile, ParseObject po, String columnName){

if(audioFile != null){
    Log.d("EB", "audioFile is not NULL: " + audioFile.toString());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedInputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(audioFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int read;
    byte[] buff = new byte[1024];
    try {
        while ((read = in.read(buff)) > 0)
        {
            out.write(buff, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] audioBytes = out.toByteArray();

    // Create the ParseFile
    ParseFile file = new ParseFile(audioFile.getName() , audioBytes);
    po.put(columnName, file);

    // Upload the file into Parse Cloud
    file.saveInBackground();
    po.saveInBackground();
}
return po;

      

}



First put the object into ParseObject, then save the file. Also, I added the ParseObject persistence (po.saveInBackground ()). Since you changed po, ​​you need to save it as well. You may have done it outside of the method, but your code you linked didn't show it.

Also, maybe try doing it in AsyncTask and call save () instead to make sure each step works (if save or something goes wrong, cancel the task) or use SaveCallback k is provided like this: ParseObject.saveInBackground (callback SaveCallback); and in the executed method call save to PO after ParseFile saved successfully.

Note: There is a 10MB ParseFile size limit, so if the audio file is larger than this, there will be a problem.

+6


source







All Articles