Convert OutputStream (to File) to Bitmap

时间:2016-10-20 18:36:36

标签: android bitmap outputstream bytearrayoutputstream

With Google Drive Rest Api I've tried to download ad image, using this.

I get a list of file with this code:

List<String> fileInfo = new ArrayList<>();
FileList result = mService.files().list()
        .setPageSize(10)
        .setQ("mimeType='image/jpeg'")
        .setFields("nextPageToken, files(id, name)")
        .execute();
        List<File> files = result.getFiles();
        if (files != null) {
            for (File file : files) {
                fileInfo.add(String.format("%s (%s)\n",
                        file.getName(), file.getId()));
            }
        }

And it works. Then, with the file id, I obtain an OutputStream with this code:

String fileId = "file_id";
OutputStream fOut = new ByteArrayOutputStream();
mService.files().export(fileId,"image/jpeg").executeMediaAndDownloadTo(fOut);

Now, how can I convert it into a file and then into a bitmap?

2 个答案:

答案 0 :(得分:6)

You can try this. byte[] bitmapdata = fOut.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

答案 1 :(得分:0)

古老的问题,但仍然有意义……也许那时不可能,但如今应该这样做:

try (InputStream is = mService.files().get(fileId).executeMediaAsInputStream()) {
    return BitmapFactory.decodeStream(is);
}