2016-10-24 3 views
0

sendbird APIでsendFileMessageを使用します。ファイルの値が必要で、drawable(またはassets)からこのファイルを使用します。sendbirdで送信できるドロウアブルからファイルを作成

この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 or uri from drawable or assets>); 
私は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){} 
uri

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

で試してみました

のようなものになる

File file = new File(path); 

私は他の目的に使用したいと私はこのコードを交換したいです

は常にファイルの取得に失敗し、エラーコードを返しますERR_REQUEST_FAILED 800220

答えて

0

以下のように試しましたか?

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

これは私に役立ちます。

+0

素晴らしい。私が望むように働く。ありがとうございました。 –

関連する問題