Telegram 如何在 Android 应用程序中发送图片和视频?

How does Telegram sends Images and Videos in Android App?

我正在开发一个消息聊天应用程序。我已经查看了 Telegram Android 应用程序的代码,但没有了解他们如何在聊天中发送图像和视频。如果您能解释一下该方法或提供任何参考,那将非常有帮助。

TIA

您可以使用 android 应用中的多部分内容发送任何类型的文件。您需要在 build.gradle

中包含这 2 个库
compile 'org.apache.httpcomponents:httpmime:4.3.6'
compile 'org.apache.httpcomponents:httpcore:4.3.3'


  private void sendImage(final String fileString, String fileMessageId)

{
    Bitmap bitmap;
    DataOutputStream dos = null;


    String sResponse = null;
    bitmap = BitmapFactory.decodeFile(fileString);

    File sourceFile = new File(fileString);


    String upLoadServerUri = WSConfig.UPLOAD_FILE_URL;
    HttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(upLoadServerUri);


    try {


        MultipartEntity entity = new MultipartEntity();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        entity.addPart("files",
                new FileBody(sourceFile));


        httpPost.setEntity(entity);
        HttpResponse response;
        try {
            response = httpClient.execute(httpPost);

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));
            sResponse = reader.readLine();




        } catch (IOException e1) {
            e1.printStackTrace();
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
}