woong's

Google Drive 사용하기(3) 다운로드 본문

Develop/Android

Google Drive 사용하기(3) 다운로드

dlsdnd345 2016. 2. 13. 23:25
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Google Drive 사용하기(3) 다운로드



다운로드 코드입니다 .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    public InputStream downloadFile(Drive service, File file) {
 
        if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
            try {
 
                HttpResponse resp =
                        service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
                                .execute();
                return resp.getContent();
            } catch (IOException e) {
                // An error occurred.
                e.printStackTrace();
                return null;
            }
        } else {
            // The file doesn't have any content stored on Drive.
            return null;
        }
    }



이건 제가 수정을 해서 사용한 다운로드 코드가 되겠습니다 .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 try {
 
                    result = new ArrayList<File>();
                    request = service[0].files().list();
 
                    FileList files = request.execute();
                    result.addAll(files.getItems());
                    request.setPageToken(files.getNextPageToken());
                } catch (IOException e) {
                    System.out.println("An error occurred: " + e);
                    request.setPageToken(null);
                }
            }
            while (request.getPageToken() != null &&
                    request.getPageToken().length() > 0)
                ;
            for (int i = 0; i < result.size(); i++) {
                if ("Book.db".equals(result.get(i).getTitle().toString())) {
                    tempfile = result.get(i);
                }
            }
 
            if (tempfile != null) {
 
                if ("Book.db".equals(tempfile.getTitle().toString())) {
 
                    String path = "data/data/com.iw.booklibrary/databases/Book.db";
                    java.io.File file = new java.io.File(path);
 
                    fin = googleUpLoadDownLoad.downloadFile(service[0], tempfile); // InputStream 반환 
 
                    try {
                        OutputStream outStream = new FileOutputStream(file);
                        byte[] buf = new byte[1024];
                        int len = 0;
 
                        if (fin != null) {
                            while ((len = fin.read(buf)) > 0) {
                                outStream.write(buf, 0, len);
                            }
                            outStream.close();
                            fin.close();
                        } else {
                            isOk = false;
                        }
                    } catch (UserRecoverableAuthIOException e) {
                        startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                        dialog.dismiss();
                        cancel(true);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    isOk = true;
 
                }
            } else {
                isOk = false;
            }



구글 드라이브에 있는 파일중에 해당되는 파일이 있으면 파일을 받아와서 로컬에 저장하는 

코드가 되겠습니다 .

'Develop > Android' 카테고리의 다른 글

Android UrQA 버그 리포트 서비스 사용하기  (0) 2016.02.13
Android Google Chart 사용하기  (0) 2016.02.13
Google Drive 사용하기(2) 업로드  (1) 2016.02.13
Google Drive 사용하기(1)  (0) 2016.02.13
Google Drive 준비 하기  (0) 2016.02.13
Comments