DownloadManager无法下载或打开谷歌驱动器URI中的内容?

时间:2016-01-25 04:46:59

标签: android android-download-manager

我正在尝试从Google云端硬盘URI下载内容但无法打开内容,但内容正在下载文件夹中下载,无论文件大小如何。虽然名为URL 2的内容已成功下载,但也在支持的应用程序中打开。请指出我缺乏的地方。我是否需要一些特殊的支付,或者可以设法管理这些代码。

如何从Google云端硬盘下载?

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 个答案:

答案 0 :(得分:2)

试一试:

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

long download_id = downloadManager.enqueue(request);

我猜您需要Google帐户的身份验证信息(会话ID等),该帐户由Cookie管理。

下载管理器不会将WebView的cookie插入其请求中,因此您需要手动将cookie值传递给请求的标头。

它对我有用。

答案 1 :(得分:0)

通过以下方法转换您的公共驱动器链接:click here

然后使用转换后的链接