我创建了一个应用程序,该应用程序从url下载文件并打开它,但是当我尝试从驱动器中下载文件时,它下载了,但是格式不同,为什么该文件无法打开,我想在其中下载驱动器文件我也尝试过PDF格式 http://docs.google.com/uc?export=download&id=FILE_ID ,但我该怎么做呢?
驱动器链接: https://drive.google.com/file/d/1cBkp1kNVnsUp13_qIuX0G-IcCVHPEqj-/view?usp=sharing
答案 0 :(得分:0)
为下载文件创建异步任务,然后单击按钮
执行BtnClick事件:
url="https://drive.google.com/file/d/1cBkp1kNVnsUp13_qIuX0G-IcCVHPEqj-/view?usp=sharing";
extension="pdf";
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFile(){
@Override
protected void onPostExecute(Long aLong) {
System.out.println("--------------Success : " + aLong);
}
}.execute(url,extension);
//file.execute(url,extension);
}
});
Asyn类:
class DownloadFile extends AsyncTask<String,Integer,Long> {
ProgressDialog mProgressDialog = new ProgressDialog(DisplayFullScreenImage.this);// Change Mainactivity.this with your activity name.
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.setMessage("Downloading");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setCancelable(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
}
@Override
protected Long doInBackground(String... aurl) {
int count;
try {
URL url = new URL((String) aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
String targetFileName=filename+"."+aurl[1];//Change name and subname
int lenghtOfFile = conexion.getContentLength();
String PATH = Environment.getExternalStorageDirectory()+ "/"+"TalentsList"+"/";
File folder = new File(PATH);
if(!folder.exists()){
folder.mkdir();//If there is no folder it will be created.
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(PATH+targetFileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress ((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return Long.valueOf(123155);
}
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
if(mProgressDialog.getProgress()==mProgressDialog.getMax()){
mProgressDialog.dismiss();
Toast.makeText(DisplayFullScreenImage.this, "File Downloaded", Toast.LENGTH_SHORT).show();
}
}
protected void onPostExecute(Long result) {
super.onPostExecute(result);
}
}
答案 1 :(得分:0)
您将无法直接使用Google api进行操作,下面是代码。
**Dependencies:**
compile 'com.google.android.gms:play-services-identity:8.3.0'
compile('com.google.api-client:google-api-client-android:1.20.0') {
exclude group: 'org.apache.httpcomponents'
}
在异步任务中执行以下操作
try{
URL url = new URL(URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "OAuth " + GoogleAuthUtil.getToken(mContext, mCredential.getSelectedAccountName(), Constants.SCOPE));
conn.setDoInput(true);
// Starts the query
conn.connect();
int responseCode = conn.getResponseCode();
//you will recive the file in input stream
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "filename.pdf");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
}