This is an example for sending a file but from my project resources. Maybe it helps…
private void sendFileUrlMessageToSendbird() {
// Make sure you have a file called "image.jpg" in your RAW folder
InputStream in = getResources().openRawResource(R.raw.image);
File f = new File(getCacheDir(), "image.jpg");
copyInputStreamToFile(in, f);
FileMessageParams params = new FileMessageParams(f)
.setFileName(f.getName());
FileMessage fileMessage = mChannel.sendFileMessage(params, new BaseChannel.SendFileMessageWithProgressHandler() {
@Override
public void onProgress(int bytesSent, int totalBytesSent, int totalBytesToSend) {
int percent = (totalBytesSent * 100) / totalBytesToSend;
Log.i(TAG, "Sending..." + percent);
}
@Override
public void onSent(FileMessage message, SendBirdException e) {
if (e != null) {
Log.i(TAG, "If WiFi is interrupted, an error will be received here");
e.printStackTrace();
} else {
Log.i(TAG, "File sent!");
}
}
});
}
private void copyInputStreamToFile(InputStream in, File file) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if ( out != null ) {
out.close();
}
in.close();
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
All the required permissions are granted. File create and upload image code is working too. Sometimes I can upload images in the open channel with the same code but most of the time it is showing an error. Can you please check the logs I have attached above?
I have used the same code in the Group channel to upload an image and it is working perfectly.
I have checked all these steps and added logs into it and I am not getting any error in any of these.
Init- Success
Connect - Success
Get channel - success
Enter channel - success
Get all messages of the channel - Success
Send Message - Getting an error.
I’ve checked the log and I can’t see the part where you enter a channel from the log, thus the error from sending a file message is logged as SendBirdException{code=900020, message=Not a member.}.