我使用以下代码为要在 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);
代码运行成功,但我认为有问题,我不知道在哪里。因为我使用以下代码来获取与文件相关的信息,而file.getThumbnail() 为 null。(getTitle() 和 getMimeType() 成功)。
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);
}
}
以下代码用于上传,成功:
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();
更新: - 我添加了这些代码来描述如何获取字节数组。
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;
}
请告诉我如何成功获得拇指指甲图像。谢谢!