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

3

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

例如文件路径为: "/storage/emulated/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;
}

谢谢!


logcat 中有任何输出吗?还是只是没有上传到 Parse? - Lucas Crawford
它只是没有上传到 Parse。 - Elad Blau
已经回答了,这应该是解决方案! - Lucas Crawford
1个回答

7

请尝试以下操作:

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 callback); 然后在done方法中,在ParseFile成功保存后调用保存到PO对象的操作。

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


没问题,我的朋友,请将其标记为被接受的答案,这样其他人如果遇到相同的问题就可以看到解决方案 :) - Lucas Crawford
我遇到了一个错误:java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.BufferedInputStream.read(byte[])' on a null object reference - grantespo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接