当我尝试使用 Facebook Graph API 和 Android 上传图像时,图像太小
简而言之,我需要使用相机捕获图像并通过我的 Android 应用程序将其上传到 Facebook。我成功地做到了。问题是,当照片发布在 Facebook 上时,它太小且分辨率低,而我拍摄的图像是高分辨率的。
我明白:为了上传到 Facebook,我需要将捕获的位图格式的图像转换为字节数组。所以我有方法:
public static byte[] convertBitmapToByteArray(Bitmap bm){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
然后将图像上传到 facebook,我在下面有代码,其中 byteData 是我使用上面的方法从位图图像转换而来的字节数组。
parameters.putString("message", "Test");
parameters.putByteArray("source", byteData);
String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST");
return facebookResponse;
我很确定问题出在我的 ConvertBitmapToByteArray 方法上,因为该方法是压缩位图图像并将其转换为字节数组,这使我的图像变成低分辨率图像。但是,我似乎无法找到在不先将图像转换为字节数组的情况下上传图像的方法。我有什么解决办法吗?
Simply put I need to capture image using camera and upload it to facebook via my android application. And I successfully did that. The problem is when the photo posted in facebook, it's just too small and in low resolution while the image I took is in high resolution.
I understand that: in order to upload to facebook, i need to convert the captured image which is in bitmap format into byte array. So i have method for that:
public static byte[] convertBitmapToByteArray(Bitmap bm){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
Then to upload the image to facebook, i have code below where byteData is byte array I converted from bitmap image using the method above.
parameters.putString("message", "Test");
parameters.putByteArray("source", byteData);
String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST");
return facebookResponse;
I am pretty sure the problem is my convertBitmapToByteArray method since the method is to compress the bitmap image and turn it into byte array, and this made my image into low resolution image. However I can't seem to find the way to upload the image without converting it into byte array first. Any solution for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,即使这个线程很旧,我也找到了答案。这不是 CompressFormant.JPEG 或 CompressFormat.JPG 的问题。简而言之,Android 中的 Intent 并不是为了在 Activity 之间传输图像等大数据而设计的。我需要先将图像从 Intent 保存到 SD 卡,然后才能从那里将其拉出。这是我的解决方案。
Alright even this thread is old, i found out the answer. It's not the problem of CompressFormant.JPEG or CompressFormat.JPG. Simply put, intent in android isn't designed to carry big data like image from activity through activity. I need to save the image from intent to sd card first before able to pull it out from there. It's my solution.