简体   繁体   中英

Set thumbnail image for uploading on Google Drive

I use the following code to set thumb nail for file to upload on Google Drive :

// Set thumb nail path                  
String thumbnail_path = mediaContent.getFile().getAbsolutePath();   
// thumbnail_path : "/sdcard/Picture/ds01.jpg" 

// File's meta data.                    
File body = new File();                 
body.setTitle(fileContent.getName());                   
body.setMimeType("image/jpeg");

// Thumb nail                   
final Thumbnail thumbnail = new Thumbnail();                    
thumbnail.setMimeType("image/jpeg");            

// UPDATE HERE : define byte array
byte[] data = Base64.decodeBase64(getData(thumbnail_path));  
thumbnail.encodeImage(Base64.encodeBase64String(data));

// set thumb nail for file
body.setThumbnail(thumbnail);

The code run successful, but I think something wrong, i don't know where. Because I used following code to get information related to file, and the file.getThumbnail() is null . ( getTitle() and getMimeType() is successful ).

private static void printFile(Drive service, String fileId) {
        try {
            File file = service.files().get(fileId).execute();

            System.out.println("Title: " + file.getTitle());
            System.out.println("MIME type: " + file.getMimeType());
            System.out.println("getThumbnail: " + file.getThumbnail());
        } catch (IOException e) {
            System.out.println("An error occured: " + e);
        }
    }

The following code is for uploading, it successful :

String folderId = ManageFile.getIdLink();
Log.d(TAG, "folderId " + folderId);
body.setParents(Arrays.asList(new ParentReference().setId(folderId)));

File file = service.files().insert(body, mediaContent).execute();

UPDATE : - I add those code for describe how to get byte array.

protected byte[] getData(String thumbnail_path) {
        byte[] imageData = null;
        final int THUMBNAIL_SIZE = 96;

        FileInputStream fis;
        try {
            fis = new FileInputStream(thumbnail_path);

            Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

            // byte data array
            imageData = baos.toByteArray();

            return imageData;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

p/s : - Get file - File resource

Please tell me how to get Thumb nail image successful . Thanks!

You need to provide the Base64 encoded image content instead of the path.

final Thumbnail thumbnail = new Thumbnail();                    
thumbnail.setMimeType("image/jpeg");            
thumbnail.setImage(base64encodedContent);

You decode the data from the getData method that are not encoded.

You should also use Base64.encodeBase64URLSafeString instead of Base64.encodeBase64String . You can use File.Thumbnail.encodeImage method as a convenience. That will do the encoding for you.

Replace this:

byte[] data = Base64.decodeBase64(getData(thumbnail_path));  
thumbnail.setImage(Base64.encodeBase64String(data));

with this:

byte[] data = getData(thumbnail_path);  
thumbnail.encodeImage(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM