如何上传音频文件到Parse.com - Android



我有一个文件的路径,我想上传到Parse.com。

例如,其中一个文件路径为:"/存储/模拟/0/app100/2015-06-04_00:45:16_RecordSound.3gp"

现在,我想把它上传到Parse.com。有解决方法吗?

我试着写一个方法,但它不工作:

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;
}

谢谢!

尝试如下:

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;

}

首先将对象放入ParseObject中,然后保存文件。此外,我还添加了保存ParseObject (po.saveInBackground())。因为您修改了po,所以也要保存。也许你在方法之外做了这个,但是你链接的代码没有显示这个

另外,也许尝试在AsyncTask中这样做并调用save()代替,以确保每个步骤都有效(如果一个保存或某些事情出错,取消任务),或使用savecallback提供如下:ParseObject。saveInBackground(SaveCallback);在done方法中,一旦ParseFile成功保存,就调用对PO对象的保存。

注意:ParseFile的大小限制为10mb,因此如果音频文件大于此值,则会出现问题。

相关内容

  • 没有找到相关文章

最新更新