// read all photos from an album URL feedUrl = "https://picasaweb.google.com/data/feed/api/user/username/albumid/albumid"; AlbumFeed feed = myService.getFeed(feedUrl, AlbumFeed.class); for(PhotoEntry photo : feed.getPhotoEntries()) { MediaSource mediaSource = photo.getMediaSource(); InputStream photoInputStream = mediaSource.getInputStream(); // read from the photoInputStream here to get contents of photo }
public static void writeTo(MediaSource source, OutputStream outputStream) throws IOException { InputStream sourceStream = source.getInputStream(); BufferedOutputStream bos = new BufferedOutputStream(outputStream); BufferedInputStream bis = new BufferedInputStream(sourceStream); long byteCounter = 0L; try { byte [] buf = new byte[2048]; // Transfer in 2k chunks int bytesRead = 0; while ((bytesRead = bis.read(buf, 0, buf.length)) >= 0) { // byte counter byteCounter += bytesRead; bos.write(buf, 0, bytesRead); } bos.flush(); } finally { bis.close(); } } }
/** * Helper method that writes the contents of a media source to an output * stream. * * @param source the source media stream containing the media data. * @param outputStream the target output stream. * @throws IOException on any error writing the data to the stream. */ public static void writeTo(MediaSource source, OutputStream outputStream) throws IOException { InputStream sourceStream = source.getInputStream(); BufferedOutputStream bos = new BufferedOutputStream(outputStream); BufferedInputStream bis = new BufferedInputStream(sourceStream); try { byte [] buf = new byte[2048]; // Transfer in 2k chunks int bytesRead = 0; while ((bytesRead = bis.read(buf, 0, buf.length)) >= 0) { bos.write(buf, 0, bytesRead); } bos.flush(); } finally { bis.close(); } } }
/** * Helper method that writes the contents of a media source to an output * stream. * * @param source the source media stream containing the media data. * @param outputStream the target output stream. * @throws IOException on any error writing the data to the stream. */ public static void writeTo(MediaSource source, OutputStream outputStream) throws IOException { InputStream sourceStream = source.getInputStream(); BufferedOutputStream bos = new BufferedOutputStream(outputStream); BufferedInputStream bis = new BufferedInputStream(sourceStream); try { byte [] buf = new byte[2048]; // Transfer in 2k chunks int bytesRead = 0; while ((bytesRead = bis.read(buf, 0, buf.length)) >= 0) { bos.write(buf, 0, bytesRead); } bos.flush(); } finally { bis.close(); } } }
URL entryUrl = new URL("https://docs.google.com/feeds/default/private/full/"+mydocumentid); DocumentEntry mydocument = client.getEntry(entryUrl, DocumentEntry.class); if (mydocument!=null) { // if we can read the document MediaContent content = (MediaContent) mydocument.getContent(); //URL exportUrl = new URL(content.getUri()); // download url MediaSource source = client.getMedia(content); InputStream inStream = null; OutputStream outStream = null; try { inStream = source.getInputStream(); outStream = resp.getOutputStream(); int c; while ((c = inStream.read()) != -1) { outStream.write(c); // copy the stream, by 1byte per 1byte is very slow ! } } finally { if (inStream != null) { inStream.close(); } if (outStream != null) { outStream.flush(); outStream.close(); } }
MediaContent mc = new MediaContent(); mc.setUri(UrlForDownload); MediaSource ms = docsService.getMedia(mc); InputStream in = ms.getInputStream();
/** * Downloads the given attachment to the given file name. */ @Override public void download(AttachmentEntry attachment, File file, SitesService sitesService) { checkNotNull(attachment); checkNotNull(file); MediaContent mediaContent = new MediaContent(); mediaContent.setUri(((OutOfLineContent) attachment.getContent()).getUri()); try { MediaSource mediaSource = sitesService.getMedia(mediaContent); InputStream inStream = mediaSource.getInputStream(); OutputStream outStream = new FileOutputStream(file); byte[] buf = new byte[4*1024]; int bytesRead; while((bytesRead = inStream.read(buf)) != -1) { outStream.write(buf, 0, bytesRead); } inStream.close(); outStream.close(); } catch (IOException e) { LOGGER.log(Level.WARNING, "Error downloading attachment: " + attachment.getTitle().getPlainText(), e); } catch (ServiceException e) { LOGGER.log(Level.WARNING, "Error downloading attachment: " + attachment.getTitle().getPlainText(), e); } } }