Rin*_*cer 3 android caching file httpclient
尝试使用Android上的HTTPClient从Internet下载并打开pdf文件.我有2节课.第一个是具有下一个方法的解析器类:
public static void saveFile(String link, FileOutputStream fos) {
DefaultHttpClient client = new DefaultHttpClient();
try{
client.setCookieStore(cookieStore);
HttpGet method = new HttpGet(link);
HttpResponse response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
entity.writeTo(fos);
fos.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
client.getConnectionManager().shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
下一课是活动.它使用已安装的软件创建一个新的Intent并打开pdf文件(用户决定使用什么软件).此活动使用Parser.class和AsyncTask中的upper方法保存pdf文件.它看起来如此:
private class AsyncExecutionOfPdf extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... arg0) {
File file;
file = new File(getCacheDir()+"/tempFile.pdf");
file.setWritable(true);
file.setReadable(true);
try {
FileOutputStream fos = new FileOutputStream(file);
Parser.saveFile("http://existingInternetFile.pdf", fos);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void resu) {
pd.dismiss();
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(getCacheDir()+"/tempFile.pdf");
Log.e("FILE EXISTS?",""+file.exists());
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(FilesActivity.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
当Log.e("FILE EXISTS?",""+ file.exists())sais,该文件存在时,它不希望用我尝试使用的8个pdf阅读器中的任何一个打开.请帮忙.谢谢
| 归档时间: |
|
| 查看次数: |
2674 次 |
| 最近记录: |