TelegramApi icon indicating copy to clipboard operation
TelegramApi copied to clipboard

Download Video and Audio and Photo

Open mehdi1602 opened this issue 7 years ago • 3 comments

Generally how can I download media(s) of a message? I know that i can call update.getMessage().getMedia(); but how i can download the photo itself?

mehdi1602 avatar Apr 16 '17 18:04 mehdi1602

if(message.hasMedia() && message.getMedia() instanceof TLMessageMediaPhoto) {
    TLPhoto photo = (TLPhoto) ((TLMessageMediaPhoto) message.getMedia()).getPhoto();

    //getting fileLocation for biggest photo
    TLFileLocation fileLocation = (TLFileLocation) ((TLPhotoSize)photo.getSizes().get(photo.getSizes().size() - 1))
            .getLocation();

    try {
        //api –– object of org.telegram.api.engine.TelegramApi class
        //Integer.MAX_VALUE – limit. Here is about 2,15GB
        //use offset for sizes more than 2,15GB
        TLFile outputFile = api.doGetFile(0, new TLInputFileLocation(fileLocation), 0, Integer.MAX_VALUE);

        //path for save image
        Files.write(Paths.get("image.jpg"), outputFile.getBytes().getData());
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
    }
}

Vyache18 avatar Apr 22 '17 23:04 Vyache18

When doing this example, it throws the following: org.telegram.api.engine.RpcException: LIMIT_INVALID at org.telegram.api.engine.TelegramApi$2.onError(TelegramApi.java:343) at org.telegram.api.engine.TelegramApi$ProtoCallback.onRpcError(TelegramApi.java:857) at org.telegram.mtproto.MTProto.onMTProtoMessage(MTProto.java:386) at org.telegram.mtproto.MTProto.onMTMessage(MTProto.java:258) at org.telegram.mtproto.MTProto.access$10(MTProto.java:236) at org.telegram.mtproto.MTProto$ResponseProcessor.run(MTProto.java:766)

Any ideas? Thanks.

robertoenr avatar May 05 '18 15:05 robertoenr

hi, try to set limit = 1024 * 32

nickdndev avatar Oct 16 '19 09:10 nickdndev