我的问题是:想从网上下载pdf文件并保存到sdcard中,但现在问题是,我所保存的pdf文件好像不是正规格式的pdf文件,以至于手机上装了几个pdf阅读器都无法打开,电脑adobe软件也无法打开,但是手机用PoQoP-Document软件可以打开
我判断是我的pdf文件生成的有问题,可能非正规pdf格式,但我得到数据流后该如何生成呢?下面是我的代码,还请高手看看,非常感谢!
File dirFile=new File(path_info);
if(!dirFile.exists()){
dirFile.mkdir();
Log.v("whether make", ""+dirFile.exists());
}
String pdf_name=urlPdf.substring(urlPdf.lastIndexOf("/")+1);
File pdf_File=new File(dirFile, pdf_name);
if(!pdf_File.exists()){
try {
pdf_File.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos=new FileOutputStream(pdf_File);
try {
URL myUrl=new URL(urlPdf);
try {
URLConnection conn=myUrl.openConnection();
HttpURLConnection httpConn=(HttpURLConnection)conn;
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
int contentLength = httpConn.getContentLength();
InputStream is=httpConn.getInputStream();
if (is == null) throw new RuntimeException("stream is null");
BufferedInputStream reader=null;
BufferedOutputStream out=null;
reader=new BufferedInputStream(is, 1024);
out=new BufferedOutputStream(fos , 1024);
byte[] bytes=new byte[512];
int length=0;
while((length=reader.read(bytes))!=-1){
out.write(bytes, 0, length);
}
fos.flush();
is.close();
fos.close();
openPDF(path_info+"/"+pdf_name);
progressD.dismiss(); }
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
我判断是我的pdf文件生成的有问题,可能非正规pdf格式,但我得到数据流后该如何生成呢?下面是我的代码,还请高手看看,非常感谢!
File dirFile=new File(path_info);
if(!dirFile.exists()){
dirFile.mkdir();
Log.v("whether make", ""+dirFile.exists());
}
String pdf_name=urlPdf.substring(urlPdf.lastIndexOf("/")+1);
File pdf_File=new File(dirFile, pdf_name);
if(!pdf_File.exists()){
try {
pdf_File.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos=new FileOutputStream(pdf_File);
try {
URL myUrl=new URL(urlPdf);
try {
URLConnection conn=myUrl.openConnection();
HttpURLConnection httpConn=(HttpURLConnection)conn;
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
int contentLength = httpConn.getContentLength();
InputStream is=httpConn.getInputStream();
if (is == null) throw new RuntimeException("stream is null");
BufferedInputStream reader=null;
BufferedOutputStream out=null;
reader=new BufferedInputStream(is, 1024);
out=new BufferedOutputStream(fos , 1024);
byte[] bytes=new byte[512];
int length=0;
while((length=reader.read(bytes))!=-1){
out.write(bytes, 0, length);
}
fos.flush();
is.close();
fos.close();
openPDF(path_info+"/"+pdf_name);
progressD.dismiss(); }
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
解决方案 »
- 怎样将一个wav文件 打包进 dpk 文件里? 还有要程序怎样访问这个打包的wav文件?
- 手机平台方面的数据库工程师有什么要求?
- listview加载位图过多导致内存溢出
- eclipse下修改APK名
- webView 播放视频问题
- 我调测百度地图定位为什么跳出一堆SQLite和exchange的错误
- Android新版ADT里使用texxtView.setText()报错,怎么解决?
- 用imageloader异步加载图片的问题,怎么实现只加载当前屏幕内的图片
- 我是一名技术小白,求一枚师傅。
- opencv算法求助
- Android2.2自带Gallery程序问题
- 关于在window7系统下安装android sdk的问题
int contentLength = httpConn.getContentLength();
InputStream is=httpConn.getInputStream();
if (is == null) throw new RuntimeException("stream is null");
byte[] bytes=new byte[1024];
int length=0;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
while((len=is.read(data))!=-1)
baos.write(data, 0, len);
fos.write(baos.toByteArray());
fos.flush();
is.close();
fos.close();
openPDF(path_info+"/"+pdf_name);
progressD.dismiss(); }
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
试试 这个!!!
if (is == null) throw new RuntimeException("stream is null"); int len = is.available();
BufferedInputStream reader=null;
BufferedOutputStream out=null;
reader=new BufferedInputStream(is, len);
out=new BufferedOutputStream(fos , len);
byte[] bytes=new byte[len];
int length=0;
while((length=reader.read(bytes))!=-1){
out.write(bytes, 0, length);
}
fos.flush();
int lenght = 0;
while ((lenght = inputStream.read(buffer)) > 0){
fos.write(buffer, 0, lenght);
}
fos 为 FileOutputStream
此方法不保证能将这些字节实际写入到物理设备(如磁盘驱动器)