【发布时间】:2012-01-11 08:21:54
【问题描述】:
我正在开发一个应用程序,我需要能够将图片上传到 picasa 打开相册。 我经历了很多线程、论坛...尝试了几种使用 http post 的方法,但似乎都没有。
以前有人做过吗?如果可以,您可以分享一个示例代码。我只需要从 picasa 进行基本的图片上传和下载。
【问题讨论】:
-
它有点像......但它不工作...... .
我正在开发一个应用程序,我需要能够将图片上传到 picasa 打开相册。 我经历了很多线程、论坛...尝试了几种使用 http post 的方法,但似乎都没有。
以前有人做过吗?如果可以,您可以分享一个示例代码。我只需要从 picasa 进行基本的图片上传和下载。
【问题讨论】:
以上答案适用于 Picasa API v2,现已弃用。我无法成功地将 Java API 用于 Picasa API v3,但我想出了一种使用 http post 将图像上传到 Picasa 的方法。这个方法我写过here:
File image = new File("/path/to/image.jpg");
byte[] imageContent = null;
try {
imageContent = Files.toByteArray(image);
} catch (Exception e) {
// do something
}
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://picasaweb.google.com/data/feed/api/user/default/albumid/default");
httpPost.addHeader("Authorization", "Bearer " + mAccessToken);
httpPost.addHeader("Content-Type", "image/jpeg");
httpPost.setEntity(new ByteArrayEntity(imageContent));
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// log the response
logd(EntityUtils.toString(httpResponse.getEntity()));
} catch (IOException e){
// do something
}
此方法使用 Apache 的 HttpClient。如果你的 Android 版本不支持,你仍然可以在 Gradle 文件中包含这一行来编译它:
compile 'cz.msebera.android:httpclient:4.4.1.1'
【讨论】:
以下问题似乎涵盖了其中的一些内容。 Picasa access in android: PicasaUploadActivity 这个线程也有信息。 http://www.mail-archive.com/android-developers@googlegroups.com/msg43707.html
它期待直接触发使用标准 picasa 上传器的意图。我将在今天晚些时候尝试将其放入我的应用程序中,因为我需要此功能。
自己做看起来是可能的,但显然更复杂的文档看起来是http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html
好的,我已经在我的应用程序中使用以下代码。这会打开 picasa 上传器。
Intent temp = new Intent(Intent.ACTION_SEND);
temp.setType("image/png");
temp.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
temp.putExtra(Intent.EXTRA_STREAM, fileUri);
temp.setComponent(new ComponentName(
"com.google.android.apps.uploader",
"com.google.android.apps.uploader.clients.picasa.PicasaSettingsActivity"));
try {
startActivity(temp);
} catch (android.content.ActivityNotFoundException ex) {
Log.v(TAG, "Picasa failed");
}
在实践中,我将取出设置组件位,让用户选择在哪里以及如何发送这是我想要的。
【讨论】: