2016-01-25 57 views
0

我試圖從Google雲端硬盤URI下載內容,但未能打開內容,但內容正在下載到具有任意大小文件的下載文件夾中。雖然名爲URL 2的內容正在成功下載,並且也在支持的應用程序中打開。請指出我缺乏的地方。我是否需要一些特殊的支付,或者可以設法這樣做。DownloadManager無法從谷歌驅動器URI下載或打開內容?

如何從Google Drive下載?

public class MainActivity extends Activity { 

// URL 1 
String Download_path = "https://drive.google.com/open?id=1bBd6Q4YyOMdYDV_v3boyYlrcecoaDXwOZw"; 
// URL 2 
//String Download_path = "http://www.vogella.de/img/lars/LarsVogelArticle7.png"; 

String Download_ID = "DOWNLOAD_ID"; 

SharedPreferences preferenceManager; 
DownloadManager downloadManager; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     preferenceManager = PreferenceManager.getDefaultSharedPreferences(this); 
     downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); 

     Button btnDownload = (Button)findViewById(R.id.download); 
     btnDownload.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    Uri Download_Uri = Uri.parse(Download_path); 
    DownloadManager.Request request = new DownloadManager.Request(Download_Uri); 
     request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI 
       | DownloadManager.Request.NETWORK_MOBILE) 
       .setAllowedOverRoaming(false) 
       .setTitle("Demo") 
       .setDescription("Something useful. No, really.") 
       .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.JPG") 
       .setMimeType("application/.jpg") 
       .allowScanningByMediaScanner(); 

     //.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension"); 

    long download_id = downloadManager.enqueue(request); 

    //Save the download id 
    Editor PrefEdit = preferenceManager.edit(); 
    PrefEdit.putLong(Download_ID, download_id); 
    PrefEdit.commit(); 
    }}); 
    } 

回答

2

試試:

String cookie = CookieManager.getInstance().getCookie(Download_path); 
request.addRequestHeader("Cookie", cookie); 

long download_id = downloadManager.enqueue(request); 

我想你需要你的谷歌賬戶,這是由餅乾管理的認證信息(會話ID,等...)。

下載管理器不會將WebView的Cookie插入到其請求中,因此您需要手動將cookie值傳遞到請求的標題。

它適用於我。

相關問題