做android客户端应用的,难免会和网络图片打交道,那么关于图片的下载和上传,以及图片的显示就会比较重要了,我刚结束了一个客户端项目,里面的主要涉及到的就是图片的处理,为此,找了很多资料,对图片的处理也有一点点经验的积累了,今天贴出来大家看看。
首先是从网络读取图片,这个还是比较简单的,网上有大量的例子可供参考,我贴出我自己写的一个方法例子出来
public static Bitmap readFromUri(String path, String urlPath)
throws Exception {
Log.d("debug","start read pic " + urlPath);
URL url = new URL(urlPath);
byte[] d = null;
/**
* 建立连接
*/
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(3 * 1000);
/**
* 得到连接状态
*/
int code = conn.getResponseCode();
Log.d("debug","code = " + code);
if (code == 200) {
/**
* 首先得到输入流,然后从输入流中写出数据到一个byte数组中,利用BitmapFactory工厂方法通过字节数组来得到
* 一个bitmap对象,最后利用FileOutputStream将bitmap对象转化成一个.png图片,存放在PIC_PATH中
*/
InputStream in = conn.getInputStream();
d = readStream(path, in);
if (d == null) {
Log.w("debug","PicUtils --- >>> d == null");
return null;
}
Bitmap bit = BitmapFactory.decodeByteArray(d, 0, d.length);
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bit.compress(Bitmap.CompressFormat.PNG, 100, out);
conn.disconnect();
out.close();
in.close();
return bit;
} else {
Log.w("debug","get Image is wrong");
}
return null;
}
private static byte[] readStream(String path, InputStream inStream) throws Exception {
if (!createFile(path)) {
Log.e("debug","create file path is not allow");
return null;
}
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outstream.write(buffer, 0, len);
}
outstream.close();
return outstream.toByteArray();
}
这样,基本就能完成图片的读取了。它返回的是一个Bitmap对象,一般应用而言,有些logo图片是没必要每次开启应用就去读取图片的,可以保存在固定的文件夹下面,上面的方法中的path就是对应这样一个文件路径的,那么下次开启应用,可以先判断图片是否存在,若存在则不读取,这样应用就可以提高速度和效率了。
下面我们来看看图片的上传,图片的上传我所了解的只有Http和Ftp,至于Ftp我还没试过,不过我相信这个是能上传的,下面我以Http上传为例子,来看看它是如何上传的:/**
* @param url 网络地址
* @param picPath 该图片所在文件夹位置
* @功能 上传图片,我的服务端是是用php写的
*/
public static String uploadPic(String url, String picPath) {
DataOutputStream dos = null; // 往服务端写入数据的输出流
try {
Log.d("debug","start upload pic " + url);
String end = "\r\n"; // 前面这些都是写html文件的头文件,固定的,不用去管
String twoHyphens = "--";
String boundary = "*****";
URL url = new URL(utl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setChunkedStreamingMode(20 * 1024);// 文件大小20K
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false); // 这个地方不使用缓存
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ picPath.substring(picPath.lastIndexOf("/") + 1)
+ "\";"
+ end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(picPath);
byte[] buffer = new byte[1024 * 2];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream()));
String result = reader.readLine(); // 这个是得到上传图片的结果
Log.d("debug","PicUtils --- >>> result = " + result);
reader.close();
httpURLConnection.disconnect();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这样就上传完毕了。