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!