2015-06-03 30 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

相關問題