Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading byte stream to Google Drive via input stream

I have media content as in byte stream, and I am attempting to upload that stream in Google Drive.

Code :

private static String writeToGoogleDrive(Drive service, String title,
        String description, String parentId, String mimeType,
        final byte[] filename) throws IOException {
    String URL="";
    File body = new File();
    body.setTitle(title);
    body.setDescription(description);
    body.setMimeType(mimeType);




    if (parentId != null && parentId.length() > 0) {
        body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
    }
    try {

    File file=  service.files().insert(body, new AbstractInputStreamContent("") {

            @Override
            public boolean retrySupported() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public long getLength() throws IOException {
                // TODO Auto-generated method stub
                System.out.println("the value of the length of the file is "+filename.length);
                return filename.length;
            }

            @Override
            public InputStream getInputStream() throws IOException {
                // TODO Auto-generated method stub
                return new ByteArrayInputStream(filename);
            }

        }).execute();

Exception: it just says null, but I don't know what (that) null is.

like image 707
user2164427 Avatar asked Oct 05 '22 03:10

user2164427


1 Answers

You can try something like this:

file = service.files().insert(body, new InputStreamContent(mimeType, 
new ByteArrayInputStream(filename))).execute();
like image 152
AKFA Avatar answered Oct 10 '22 01:10

AKFA