androidbitmapoutputstreambytearrayoutputstream

Convert OutputStream (to File) to Bitmap


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?


Solution

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