{"_id":"5703e589a7472a0e00b6de39","__v":1,"category":{"_id":"5703d8b7aceacc17003ef303","version":"5703d527bb69fc1700553ce3","__v":0,"project":"5703d527bb69fc1700553ce0","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-05T15:24:39.108Z","from_sync":false,"order":1,"slug":"samples","title":"Samples"},"version":{"_id":"5703d527bb69fc1700553ce3","hasDoc":true,"__v":6,"hasReference":true,"project":"5703d527bb69fc1700553ce0","createdAt":"2016-04-05T15:09:27.620Z","releaseDate":"2016-04-05T15:09:27.620Z","categories":["5703d527bb69fc1700553ce4","5703d8b7aceacc17003ef303","5703e60b6116142000db25f6","57e0db616a1c2e0e0081fe64","57e0f141ff540c22007b45fa","57e0f14b8929550e00f1d9bc"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.0.6","version":"0.0.6"},"project":"5703d527bb69fc1700553ce0","parentDoc":null,"githubsync":"","user":"5631f962c3b04b0d00ba9bf1","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-05T16:19:21.735Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"This snippet will get the most recent conversation and download the media attached with the message (if any).\nThe media can be a file, a photo, a **sticker**, a gif, etc.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// This is a synchronous client, that will block until the response arrive (or until timeout)\\nTelegramClient client = Kotlogram.getDefaultClient(application, new ApiStorage());\\n\\n// You can start making requests\\ntry {\\n    TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), 1);\\n    TLAbsInputPeer inputPeer = getInputPeer(tlAbsDialogs);\\n\\n    // Get most recent message\\n    TLAbsMessages tlAbsMessages = client.messagesGetHistory(inputPeer, 0, 0, 0, 1, 0, 0);\\n    TLAbsMessage tlAbsMessage = tlAbsMessages.getMessages().get(0);\\n\\n    if (tlAbsMessage instanceof TLMessage && ((TLMessage) tlAbsMessage).getMedia() != null) {\\n        TLAbsMessageMedia media = ((TLMessage) tlAbsMessage).getMedia();\\n\\n        // Magic utils method from api module\\n        MediaInput mediaInput = TLMediaUtilsKt.getAbsMediaInput(media);\\n\\n        if (mediaInput != null) {\\n            String filename;\\n            if (media instanceof TLMessageMediaPhoto || media instanceof TLMessageMediaWebPage) {\\n                filename = \\\"photo.jpg\\\";\\n            } else {\\n                // Retrieve real name\\n                TLDocument tlDocument = ((TLMessageMediaDocument) media).getDocument().getAsDocument();\\n                filename = ((TLDocumentAttributeFilename) tlDocument.getAttributes().stream()\\n                                                                    .filter(attr -> attr instanceof TLDocumentAttributeFilename)\\n                                                                    .findFirst().get()).getFileName();\\n            }\\n\\n            FileOutputStream fos = new FileOutputStream(new File(ROOT_DIR, filename));\\n            client.downloadSync(mediaInput.getInputFileLocation(), mediaInput.getSize(), fos);\\n            // downloadSync closes the stream automatically\\n        } else {\\n            System.err.println(\\\"MessageMedia type not supported\\\" + media.getClass().getSimpleName());\\n        }\\n    } else {\\n        System.err.println(\\\"Latest message has no media attached\\\");\\n    }\\n} catch (RpcErrorException | IOException e) {\\n    e.printStackTrace();\\n} finally {\\n    client.close(); // Important, do not forget this, or your process won't finish\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nThis tl-schema of the API can sometime complicates things a lot with a lot of abstraction, moreover when dealing with them in Java.","excerpt":"[See on github](https://github.com/badoualy/kotlogram/blob/master/sample/src/main/java/com/github/badoualy/telegram/sample/SampleDownloadMessageMedia.java)","slug":"download-a-picture-from-a-message","type":"basic","title":"Download a media from a message"}

Download a media from a message

[See on github](https://github.com/badoualy/kotlogram/blob/master/sample/src/main/java/com/github/badoualy/telegram/sample/SampleDownloadMessageMedia.java)

This snippet will get the most recent conversation and download the media attached with the message (if any). The media can be a file, a photo, a **sticker**, a gif, etc. [block:code] { "codes": [ { "code": "// This is a synchronous client, that will block until the response arrive (or until timeout)\nTelegramClient client = Kotlogram.getDefaultClient(application, new ApiStorage());\n\n// You can start making requests\ntry {\n TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), 1);\n TLAbsInputPeer inputPeer = getInputPeer(tlAbsDialogs);\n\n // Get most recent message\n TLAbsMessages tlAbsMessages = client.messagesGetHistory(inputPeer, 0, 0, 0, 1, 0, 0);\n TLAbsMessage tlAbsMessage = tlAbsMessages.getMessages().get(0);\n\n if (tlAbsMessage instanceof TLMessage && ((TLMessage) tlAbsMessage).getMedia() != null) {\n TLAbsMessageMedia media = ((TLMessage) tlAbsMessage).getMedia();\n\n // Magic utils method from api module\n MediaInput mediaInput = TLMediaUtilsKt.getAbsMediaInput(media);\n\n if (mediaInput != null) {\n String filename;\n if (media instanceof TLMessageMediaPhoto || media instanceof TLMessageMediaWebPage) {\n filename = \"photo.jpg\";\n } else {\n // Retrieve real name\n TLDocument tlDocument = ((TLMessageMediaDocument) media).getDocument().getAsDocument();\n filename = ((TLDocumentAttributeFilename) tlDocument.getAttributes().stream()\n .filter(attr -> attr instanceof TLDocumentAttributeFilename)\n .findFirst().get()).getFileName();\n }\n\n FileOutputStream fos = new FileOutputStream(new File(ROOT_DIR, filename));\n client.downloadSync(mediaInput.getInputFileLocation(), mediaInput.getSize(), fos);\n // downloadSync closes the stream automatically\n } else {\n System.err.println(\"MessageMedia type not supported\" + media.getClass().getSimpleName());\n }\n } else {\n System.err.println(\"Latest message has no media attached\");\n }\n} catch (RpcErrorException | IOException e) {\n e.printStackTrace();\n} finally {\n client.close(); // Important, do not forget this, or your process won't finish\n}", "language": "java" } ] } [/block] This tl-schema of the API can sometime complicates things a lot with a lot of abstraction, moreover when dealing with them in Java.