2015-06-03 76 views
3

我有一个文件的路径,我想上传到Parse.com。如何将音频文件上传到Parse.com - Android

例如,文件路径之一是: “/storage/emulated/0/app100/2015-06-04_00:45:16_RecordSound.3gp”

现在,我要上传到解析.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; 
} 

谢谢!

+0

有来自logcat的任何输出,或只是不上传到解析 –

+0

它只是没有上传来解析。 –

+0

回答下面,这应该是解决方案! –

回答

6

尝试以下操作:

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())。既然你修改了宝,你也必须保存它。也许你是在方法之外做的,但是你链接的代码没有显示这个。

此外,也许尝试在AsyncTask中执行此操作并调用save(),以确保每个步骤都能正常工作(如果保存或出错,取消任务)或使用以下提供的SaveCallback k :ParseObject.saveInBackground(SaveCallback callback);并在完成方法调用ParseFile成功保存后保存到PO对象。

注:没有为10MB的ParseFile大小的限制,因此,如果音频文件是比这大,会有一个问题。

+0

谢谢你! 它现在有效! –

+0

没问题我的朋友,将其标记为接受的答案,以便其他人可以看到解决你的问题,如果他们有同样的问题:) –

+0

我得到一个错误:'显示java.lang.NullPointerException:试图调用虚拟方法INT的java .io.BufferedInputStream.read(byte [])'在空对象引用上 – grant

相关问题