问题描述
我用下面的code为设置缩略图文件上传在谷歌驱动器:
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);
在code运行成功,但我认为有些事情错了,我不知道在哪里。因为我用下面的code去文件的相关信息,并在 file.getThumbnail()为null 。 (的getTitle()和getMimeType()是成功的的)。
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);
}
}
下面code是上传,它成功的:
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();
更新: - 我想补充的code代表介绍了如何获得字节数组
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!
推荐答案
您c从的getData code>方法是不连接codeD中的数据去$ C $ 。
You decode the data from the getData
method that are not encoded.
您应该还可以使用 Base64.en codeBase64URLSafeString
,而不是 Base64.en codeBase64String
的。您可以使用<一个href="https://developers.google.com/resources/api-libraries/documentation/drive/v2/java/latest/com/google/api/services/drive/model/File.Thumbnail.html#en$c$cImage%28byte%5B%5D%29"相对=nofollow> File.Thumbnail.en $ C $的CImage 作为一种方便的方法。这将做编码为你。
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.
替换这样的:
byte[] data = Base64.decodeBase64(getData(thumbnail_path));
thumbnail.setImage(Base64.encodeBase64String(data));
这一点:
byte[] data = getData(thumbnail_path);
thumbnail.encodeImage(data);
这篇关于关于谷歌驱动器上传设置缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!