gpt4 book ai didi

java - 文件部分是从 Android Webview 下载的

转载 作者:行者123 更新时间:2023-12-01 13:25:42 26 4
gpt4 key购买 nike

我在点击 Web View 中的按钮下载 PDF 文件时遇到问题。

文件已下载,但文件已部分下载,这就是我收到以下错误的原因

“该文档无法打开,因为它不是有效的 PDF 文档”

下面是我下载文件的 Asyncetask Activity :

 public class DownloadPDFTask extends AsyncTask<String, Void, Integer> 
{
protected ProgressDialog mWorkingDialog; // progress dialog
protected String mFileName; // downloaded file
protected String mError; // for errors

@Override
protected Integer doInBackground(String... urls)
{
String filename = "";
String str[] = urls[2].split(";");
String st[] =str[1].split("=");
filename = st[1];
String extStorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File myDir = new File(extStorageDirectory, "NCR");

File file = new File(extStorageDirectory+"/NCR/"+filename);
// create the directory if it does not exist
if (!myDir.exists())
myDir.mkdirs();

if (file.exists()) {
System.out.println("INSIDE FILE EXIST");
file.delete();
}
try
{
byte[] dataBuffer = new byte[4096];
int nRead = 0;

mFileName = filename;

System.out.println("mFileName<><><> " +mFileName);
// download URL and store to strFileName

// connection to url
java.net.URL urlReport = new java.net.URL(urls[0]);
URLConnection urlConn = urlReport.openConnection();
urlConn.setRequestProperty("User-Agent", urls[1]);
urlConn.setRequestProperty("Content-Disposition", urls[2]);
urlConn.setRequestProperty("Content-Type", "application/pdf");
urlConn.setRequestProperty("Accept", "*/*");
InputStream streamInput = urlReport.openStream();
BufferedInputStream bufferedStreamInput = new BufferedInputStream(streamInput,8192);
FileOutputStream outputStream = new FileOutputStream(extStorageDirectory+"/NCR/"+mFileName);
while ((nRead = bufferedStreamInput.read(dataBuffer)) > 0)
outputStream.write(dataBuffer, 0, nRead);
streamInput.close();
outputStream.close();
// displayPdf(mFileName);

}
catch (Exception e)
{
Log.e("myApp", e.getMessage());
mError = e.getMessage();
return (1);
}

return (0);
}



@Override
protected void onPreExecute()
{
// show "Downloading, Please Wait" dialog
mWorkingDialog = ProgressDialog.show(context, "", "Downloading PDF Document, Please Wait...", true);
return;
}



@Override
protected void onPostExecute (Integer result)
{
if (mWorkingDialog != null)
{
mWorkingDialog.dismiss();
mWorkingDialog = null;
}

switch (result)
{
case 0: // a URL
try
{

displayPdf(mFileName);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(context, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}

break;

case 1: // Error

Toast.makeText(context, mError, Toast.LENGTH_LONG).show();
break;

}

}

}

friend 们,我被困在这个问题上,请帮助我。

最佳答案

希望这对您有帮助。我测试了这段代码,效果很好。

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
int count;
try{
URL url=new URL(params[0]);
URLConnection connection=url.openConnection();
connection.connect();
//getting file length
long lengthOfFile=connection.getContentLength();
//input stream to read file with 8k buffer
InputStream input=new BufferedInputStream(url.openStream(),8192);
//out stream to write file
OutputStream output=new FileOutputStream(Environment.getExternalStorageDirectory()+"/Download/Test/software_testing.pdf");

byte data[]= new byte[1024];
long total =0;
while ((count = input.read(data)) != -1){
if(isCancelled())
return null;
total +=count;
//publishing the progress
//After this onProgressUpdate will be called
if(lengthOfFile > 0){
//System.out.println((int)((total*100)/lengthOfFile)+"First Line");
//Call onProgressUpdate() for display status
publishProgress((int)((total*100)/lengthOfFile));

}

//writing data to file
output.write(data,0,count);
}
//flushing output
output.flush();
//closing stream
output.close();
input.close();

}catch(Exception e){
Log.e("Error", e.getMessage());
System.out.println("Exception :"+e.getMessage());
}

return null;

}

编辑:

AsyncTask<String, Integer, String> 扩展您的类(class)并覆盖它的方法。`

  • onPreExecute()用于在开始下载之前进行处理。
  • doInBackground(String... params)用来做这个过程下载文件。上面的代码就是针对这个方法的。
  • onProgressUpdate(Integer... progress)用于设置根据当前下载百分比显示进度条。一旦你使用publishProgress(),这个方法就会被调用。

  • onPostExecute(String file_url)这个方法可以用来消除下载文件后dislog。

所以你要做的就是设置你的进度条根据onProgressUpdate (Integer... progress)里面的下载百分比来更新。您可以使用setProgress()方法。

我希望你现在能很好地理解这个过程:)

关于java - 文件部分是从 Android Webview 下载的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21817860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com