【发布时间】:2018-02-08 05:50:14
【问题描述】:
我想从谷歌驱动器下载文件,我正在使用 compile 'com.google.android.gms:play-services:8.4.0' 的依赖项,并使用它可以从下面的示例中获取元数据的链接。
mFileId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
final DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, mFileId);
new Thread(new Runnable() {
@Override
public void run() {
// DO your work here
DriveResource.MetadataResult mdRslt = file.getMetadata(mGoogleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
String link = mdRslt.getMetadata().getWebContentLink();
String name=mdRslt.getMetadata().getTitle();
Log.d("LINK", link);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getApplication().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else
{
new get_download_data(link,name).execute();
}
}
}
}).start();
}
从 Google Drive 获取链接后,我正在调用异步任务从链接下载该文件。所以我的问题是当我下载文件时,它没有打开。经过检查和调试,我发现我的文件没有正确下载。
例如,我有文件名abc.pdf,大小为 400kb。我在我的 SD 卡上下载了,但 abc.pdf 只有 56 kb。我正在使用以下代码进行下载。我不知道我在哪里做错了。请帮忙。谢谢。
public class get_download_data extends AsyncTask<Void,Void,String>
{
File apkStorage = null;
File outputFile = null;
String link1="";
String name1="";
public get_database_data(String link, String name) {
this.link1=link;
this.name1=name;
}
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(link1);//Create Download URl
HttpURLConnection c = (HttpURLConnection) url.openConnection();//Open Url Connection
c.setRequestMethod("GET");//Set Request Method to "GET" since we are grtting data
c.setDoInput(true);
c.connect();//connect the URL Connection
//If Connection response is not OK then show Logs
if (c.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
+ " " + c.getResponseMessage());
}else{
Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
+ " " + c.getResponseMessage());
}
//Get File if SD card is present
if (new CheckForSDCard().isSDCardPresent()) {
apkStorage = new File(
Environment.getExternalStorageDirectory() + "/"
+ "checkdb");
} else
Toast.makeText(getApplication(), "Oops!! There is no SD Card.", Toast.LENGTH_SHORT).show();
//If File is not present create directory
if (!apkStorage.exists()) {
apkStorage.mkdir();
Log.e(TAG, "Directory Created.");
}
outputFile = new File(apkStorage, name1);//Create Output file in Main File
//Create New File if not present
if (!outputFile.exists()) {
outputFile.createNewFile();
Log.e(TAG, "File Created");
}
OutputStream fos = new FileOutputStream(outputFile);//Get OutputStream for NewFile Location
InputStream is = c.getInputStream();//Get InputStream for connection
BufferedInputStream inStream = new BufferedInputStream(is, 1024);
byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
while ((len1 = inStream.read(buffer)) != -1) {
fos.write(buffer, 0, len1);//Write new file
}
//Close all connection after doing task
fos.flush();
fos.close();
is.close();
} catch (Exception e) {
//Read exception if something went wrong
e.printStackTrace();
outputFile = null;
Log.e(TAG, "Download Error Exception " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result_1) {
super.onPostExecute(result_1);
String downlodepath = Environment.getExternalStorageState()+"/"+name1;
Log.e("Sdpath",""+imagePath);
Toast.makeText(getApplication(), "download"+downlodepath, Toast.LENGTH_SHORT).show();
}
}
我找到了这个链接here,但有些我不知道如何实现它。请让我知道我错在哪里。谢谢。
【问题讨论】:
标签: android android-fragments google-drive-api