ImageIO-图片存取

最近在做图片读写,从服务器上读一张图片,存到手机的SD卡上。一开始是用的FileInputStream和FileOutputStream,但很多时候图片都损坏了,我也不明白是什么原因。有人建议我用ImageReader一系列的东西,我就尝试了一下。但是报出了这么一个错误:java.lang.NoClassDefFoundError:javax.imageio.ImageIO

我看了一下ImageIO在JRE System library中,我用的是jdk1.6.0_10。从网上搜了一下错误,说是包重复,我这也没发现重复现象。所以请各位高手半个帮,我做开发时间不长,还请多多帮助。另外,存取图片,怎样解决图片失真,损坏问题呢?

代码1:获取服务器连接

public InputStream getInputStream(String serverpath) throws Exception{
		InputStream in=null;
		
		URL url = new URL(serverpath);
		httpurlconnection = (HttpURLConnection) url
				.openConnection();
		httpurlconnection.setRequestMethod("POST");
		httpurlconnection.setRequestProperty("Content-Type",
				"text/html");
		httpurlconnection.setConnectTimeout(1000);
		httpurlconnection.setDoOutput(true);
		httpurlconnection.setDoInput(true);
		httpurlconnection.setUseCaches(false);
		httpurlconnection.connect();
		System.out.println(123);
		in=url.openStream();
		System.out.println(456);
		return in;
	}

 代码2:存取图片

public void savepic(List<String> list,String packagename) throws Exception{
		String filename="";
		String url="";
		for(int i=0;i<list.size();i++){
			/*
			 * ---第一张图片:图标      xxx0.png
			 * ---第二张图片:大图1    xxx1.png
			 * ---第三张图片:大图2    xxx2.png
			 * ---第四张图片:star全   xxx3.png
			 * ---第五张图片:star半   xxx4.png
			 */
			filename=list.get(i);
			System.out.println(filename.split("\\.")[0]);
			url="http://www.gyswad.com:90/push/"+filename;

			InputStream inputStream=getInputStream(url);
			
			FileOutputStream output = null;
//			ByteArrayOutputStream out=null;
			try {
				// 存储到SD卡
				File sdFile = new File(sdcard_path + "/"+packagename+"/"+ filename.split("\\.")[0]+".gif");
				File f1 = new File(sdcard_path);
				File f2=new File(sdcard_path+"/"+packagename);
				if (!f1.exists()) {
					f1.mkdir();// 创建文件夹
				}
				if(!f2.exists()){
					f2.mkdir();
				}
				if (!sdFile.exists()) {
					sdFile.createNewFile();// 创建文件
					// 写入文件
					Iterator<ImageReader> readers=ImageIO.getImageReadersByFormatName("gif");
					ImageReader reader=readers.next();
					
					ImageInputStream iis=ImageIO.createImageInputStream(inputStream);
					reader.setInput(iis,true);
					
					BufferedImage bi=reader.read(0);
					
					Iterator<ImageWriter> writers=ImageIO.getImageWritersByFormatName("gif");
					ImageWriter writer=writers.next();
					
					ImageOutputStream ios=ImageIO.createImageOutputStream(sdFile);
					writer.setOutput(ios);
					
					writer.write(bi);
					//之前是用的这种方式
//					output = new FileOutputStream(sdFile);
//					byte buffer[] = new byte[inputStream.available()];
//					while (inputStream.read(buffer,0,buffer.length) != -1) {
//						output.write(buffer);
//					}
//					iis.close();
//					ios.close();
				}
//				if(output!=null){					
//					output.flush();
//				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("savepic Exception:"+e);
			} finally {
				if(inputStream!=null){
					inputStream.close();
				}
				if(output!=null){				
					output.close();
				}
				if(httpurlconnection!=null){
					httpurlconnection.disconnect();
				}
			}
		}
	}

ImageIO-图片存取

相关推荐