Reputation: 161
I have a path of a file that I want to upload to Parse.com.
For example, one of the file paths is: "/storage/emulated/0/app100/2015-06-04_00:45:16_RecordSound.3gp"
Now, I want to upload it to Parse.com. Any solution how to do it?
I've tried to write a method for this, but it's not working:
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;
}
Thanks!
Upvotes: 3
Views: 1255
Reputation: 3118
Try the following:
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;
}
Put the object in the ParseObject first, then save the file. Also, I added saving the ParseObject as well (po.saveInBackground()). Since you modified po, you have to save it as well. Maybe you did this outside the method, but your code you linked didn't show this.
Also, maybe try doing this in an AsyncTask and calling save() instead, in order to make sure each step works (if one save or something goes wrong, cancel the task), or use the SaveCallbackk provided as follows: ParseObject.saveInBackground( SaveCallback callback); and in the done method call the save to the PO object once the ParseFile successfully saves.
Note: there is a limit to ParseFile size of 10mb, so if the audio file is larger than this, there will be an issue.
Upvotes: 7