`
pure
  • 浏览: 350685 次
社区版块
存档分类
最新评论

使用Google 相册API

阅读更多
在做GAE应用时,想上传图片在blog中使用,当然可以直接上传到相册,然后复制链接也是可行的,既然google相册提供了API,用api是不是更方便呢?上传完相片完马上就能知道相片地址了。

首先要下载gdata-java-client lib与相关依赖的lib

http://code.google.com/p/gdata-java-client/downloads/list



package sample.photos;

import java.io.File;
import java.net.URL;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;

public class UploadPhoto {

	public void createPhoto() {
		PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
		try {
			myService.setUserCredentials("xxxx@gmail.com", "xxxx!@#");
			URL feedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/xxxx?kind=album");
			UserFeed myUserFeed = myService.getFeed(feedUrl, UserFeed.class);
			for (AlbumEntry myAlbum : myUserFeed.getAlbumEntries()) {
				System.out.println(myAlbum.getTitle().getPlainText());
			}

			URL albumPostUrl = new URL("http://picasaweb.google.com/data/feed/api/user/xxxx/albumid/5358938369305614721");
			PhotoEntry myPhoto = new PhotoEntry();
			myPhoto.setTitle(new PlainTextConstruct("Puppies FTW"));
			myPhoto.setDescription(new PlainTextConstruct("Puppies are the greatest."));
			myPhoto.setClient("myClientName");
			MediaFileSource myMedia = new MediaFileSource(new File("F:/mm/1.jpg"), "image/jpeg");
			myPhoto.setMediaSource(myMedia);
			PhotoEntry returnedPhoto = myService.insert(albumPostUrl, myPhoto);
			System.out.println(returnedPhoto.getHtmlLink().getHref());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		UploadPhoto upLoadPhoto = new UploadPhoto();
		upLoadPhoto.createPhoto();

	}

}


这下操作google相册就方便很多了,晚上测试一下在GAE中能否使用这些API,顺利的话,下一步工作就更简单了。
分享到:
评论
3 楼 Laynepeng 2009-07-17  
果然是GWF:
http://www.google.com/support/forum/p/other/thread?tid=56287de5d50ebaee&hl=zh-CN
2 楼 Laynepeng 2009-07-17  
照片能顺利传上去,但是那伟大的wall把picasa给封了,至少上海长城宽带上不了~~郁闷!
1 楼 Laynepeng 2009-07-17  
其实不用那么麻烦,这样写就好了,

public String addAPhoto(String filePath, String userId, String password, String albumnName) throws IOException, ServiceException {
        PicasawebClient picasawebClient = new PicasawebClient(picasawebService, userId, password);
        List<AlbumEntry> albums = picasawebClient.getAlbums();
        AlbumEntry blogAlbumnEntry = null;
        for (Iterator<AlbumEntry> albumnIt = albums.iterator(); albumnIt.hasNext();) {
            AlbumEntry albumn = albumnIt.next();
            if (albumnName.equalsIgnoreCase(albumn.getName())) {
                blogAlbumnEntry = albumn;
                break;
            }
        }
        if (null != blogAlbumnEntry) {
            String feedUrl = picasawebClient.getLinkByRel(blogAlbumnEntry.getLinks(), Link.Rel.FEED);
            MediaFileSource myMedia = new MediaFileSource(new File(filePath), "image/jpeg");
            PhotoEntry returnedPhoto = picasawebService.insert(new URL(feedUrl), PhotoEntry.class, myMedia);
            return returnedPhoto.getMediaThumbnails().get(0).getUrl();
        }
    }

相关推荐

Global site tag (gtag.js) - Google Analytics