从可绘制文件创建文件以使用sendbird发送 [英] Create file from drawable to send with sendbird

查看:126
本文介绍了从可绘制文件创建文件以使用sendbird发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在sendbird api中使用 sendFileMessage .它需要文件值,我想使用 drawable (或 assets )中的此文件. sendBird API

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

这是sendbird中的代码片段

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();
    }
});

此代码运行良好,我从打开的图像意图中获得了 uri .但是我想用于其他用途,并且我想替换此代码

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);

成为类似

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

我已经尝试过 uri

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

使用inputStream

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){}  

始终无法获取文件并返回错误代码 ERR_REQUEST_FAILED 800220

always failed in getting file and return error code ERR_REQUEST_FAILED 800220

推荐答案

您是否尝试过以下方法?

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) {
    }
});

这对我有用.

这篇关于从可绘制文件创建文件以使用sendbird发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆