[英]android cant open pdf from cache dir
尝试使用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();
}
}
下一课是活动。 它使用已安装的软件创建一个新的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();
}
}
}
当Log.e(“FILE EXISTS?”,“”+ file.exists())sais,该文件存在时,它不希望用我尝试使用的8个pdf阅读器中的任何一个打开。 请帮忙。谢谢
那些“8个pdf阅读器”无法访问您的缓存目录。 将其存储在外部存储(例如, getExternalCacheDir()
)上,或创建ContentProvider
以从内部缓存目录中提供PDF。 此示例项目演示如何从内部存储提供文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.