我正在尝试使用fileMetadata.getWebContentLink()
当我打印此链接时,它显示如下内容:
https://drive.google.com/uc?id=0Bw6vr2LNxB3iUFJrTk5oZDljaTA&export=download
当我将此链接放入桌面浏览器时,文件会自动下载。但我无法将其下载到我的应用程序中。
我使用此link 使用new DownloadFileAsync().execute(fileMetadata.getWebContentLink());
当我将文件公开以便在我的Google驱动器中访问时,它会正常工作,否则它无法正常工作。 任何人都可以知道为什么会发生这种情况以及如何解决这个问题?
答案 0 :(得分:1)
WebcontentLink仅适用于浏览器。
以下是按照here
的说明在您的应用中下载文件的步骤答案 1 :(得分:-3)
按照以下步骤下载随机文件。创建一个名为DownloadFileAsync.java的类
public class DownloadFileAsync extends AsyncTask<String, String, String> {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private Context context;
public DownloadFileAsync(Context context)
{
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(aurl[0]);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(root + "/", aurl[1]));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
}
}
将您的活动中的上述课程称为
new DownloadFileAsync(TransformerActivity.this).execute(Constants.VIDEO_DOWNLOAD_LINK,"9T51B0108.mp4");