Create file from drawable to send with sendbird

邮差的信 提交于 2019-12-25 07:16:41

问题


I want use sendFileMessage in sendbird api. It need file value and I want use this file from drawable (or assets). sendBird API

this is snipped code from sendbird

Hashtable<String, Object> info = Helper.getFileInfo(getActivity(), uri);
final String path = (String) info.get("path");
File file = new File(path);
String name = file.getName();
String mime = (String) info.get("mime");
int size = (Integer) info.get("size");

sendFileMessage(file, name, mime, size, "", new BaseChannel.SendFileMessageHandler() {
    public void onSent(FileMessage fileMessage, SendBirdException e) {
        if (e != null) {
            return;
        }
        mAdapter.appendMessage(fileMessage);
        mAdapter.notifyDataSetChanged();
    }
});

This code working well which I got uri from open image intent. but I want to use to other purpose and I want to replace this code

File file = new File(path);

become something like

File file = new File(<path or uri from drawable or assets>);

I have tried with uri

Uri uri = Uri.parse("android.resource://com.package.name/raw/filenameWithoutExtension");
File file = new File(uri.getPath());  

with inputStream

try {
    File f=new File("file name");
    InputStream inputStream = getResources().openRawResource(R.raw.myrawfile);
    OutputStream out=new FileOutputStream(f);
    byte buf[]=new byte[1024];
    int len;
    while((len=inputStream.read(buf))>0)
    out.write(buf,0,len);
    out.close();
    inputStream.close();
}
catch (IOException e){}  

always failed in getting file and return error code ERR_REQUEST_FAILED 800220


回答1:


Have you tried like below?

String fileName = FILE_NAME;
File cachedFile = new File(this.getActivity().getCacheDir(), fileName);

try {
    InputStream is = getResources().openRawResource(R.raw.sendbird_ic_launcher);
    FileOutputStream fos = new FileOutputStream(cachedFile);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0)
        fos.write(buf, 0, len);
        fos.close();
        is.close();
} catch (IOException e) {
    e.printStackTrace();
}

groupChannel.sendFileMessage(cachedFile, fileName, "image/jpg", (int) cachedFile.length(), "", new BaseChannel.SendFileMessageHandler() {
    @Override
    public void onSent(FileMessage fileMessage, SendBirdException e) {
    }
});

This works for me.



来源:https://stackoverflow.com/questions/40214136/create-file-from-drawable-to-send-with-sendbird

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!