我的问题是:想从网上下载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();
}
解决方案 »
- 内存不足10000-byte external allocation too large for this process
- android:process 问题
- android 音乐播放器问题
- mmc代码的疑惑,mmc_wait_for_cmd函数
- 请教:铃音裁剪
- 平台为Beagleboard xm Rev B系统为android2.3如何添加etherent?
- Url获取InputStream 空指针异常
- 哥跳槽找工作,2年Android应用开发(含云计算),在深圳 能开多少薪水?
- 超级闹钟源代码及应用程序
- 如何实现线条的动态移动,一端固定,另一端移动的功能
- 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
此方法不保证能将这些字节实际写入到物理设备(如磁盘驱动器)