画像のアップロードとダウンロード

1704 ワード

簡単な画像処理:
/**
	 *  
	 * 
	 * @param fileName
	 * @param url
	 * @return
	 */
	public static boolean httpPostBase64(String fileName) {
		try {
			File file = new File(fileName);
			FileInputStream in = new FileInputStream(file);
			byte[] buffer = new byte[(int) file.length() + 100];
			int length = in.read(buffer);
			String data = Base64.encodeToString(buffer, 0, length,
					Base64.DEFAULT);
			HttpPost httpRequest = new HttpPost(requestHead
					+ "editeuserheadimg.php");
			/*
			 * NameValuePair 
			 */
			List<NameValuePair> params = new LinkedList<NameValuePair>();
			params.add(new BasicNameValuePair("imagestring", data));
			params.add(new BasicNameValuePair("phonenumber",
					DemoApplication.currentUserName.split("@coboqo")[0]));
			/*   */
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			/*   */
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequest);
			/*  200 ok */
			if (httpResponse.getStatusLine().getStatusCode() != 200) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 *  
	 * 
	 * @param imgpath
	 * @return
	 */
	public static Bitmap httpGetImg(String imgpath) {
		String web = requestHead + imgpath;
		InputStream instream;
		Bitmap bitmap = null;
		try {
			URL url = new URL(web);
			URLConnection con = url.openConnection();
			con.setDoInput(true);
			con.connect();
			instream = con.getInputStream();
			bitmap = BitmapFactory.decodeStream(instream);
			instream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bitmap;
	}