ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-24 16:26:12  阅读:180  来源: 互联网

标签:parse-platform android file audio upload


我有一个文件路径,我想上传到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;
}

谢谢!

解决方法:

请尝试以下方法:

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(),以确保每个步骤都有效(如果一个保存或出现问题,取消任务),或使用提供的SaveCallbackk如下:ParseObject.saveInBackground( SaveCallback回调);并且在完成方法中,一旦ParseFile成功保存,就将保存调用到PO对象.

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

标签:parse-platform,android,file,audio,upload
来源: https://codeday.me/bug/20190824/1709921.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有