【发布时间】:2016-11-04 20:13:50
【问题描述】:
我想在 sendbird api 中使用sendFileMessage。它需要文件值,我想使用来自drawable(或assets)的这个文件。 sendBird API
这是从 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。但我想用于其他目的,我想替换这段代码
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());
使用输入流
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
【问题讨论】:
标签: android android-file sendbird