rubenlagus / TelegramApi

Java library to create Telegram Clients
MIT License
297 stars 108 forks source link

Download Video and Audio and Photo #24

Open mehdi1602 opened 7 years ago

mehdi1602 commented 7 years ago

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?

Vyache18 commented 7 years ago
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();
    }
}
robertoenr commented 6 years ago

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.

nickdndev commented 4 years ago

hi, try to set limit = 1024 * 32