做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();
}
}
}
}
這樣就上傳完畢了。