android

主要记录下主要代码留作日后参考


Android


//Bitmap bitmap = BitmapFactory.decodeResource(
  //   getResources(), R.drawable.ic_launcher);
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)) {
String ss = Base64.encodeToString(baos.toByteArray(),
Base64.DEFAULT);
String request = UploadUtil.uploadFile(ss, requestURL);
                    }
  public static String uploadFile(String param, String RequestURL) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
// URL realUrl = new URL(url);
URL realUrl = new URL(
"http://192.168.0.1/xxxxx/xxxxx.php");
//Log.i("url", url + "?" + param);
URLConnection conn = realUrl.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
String cookieval = conn.getHeaderField("set-cookie");
//String sessionid = null;
if (cookieval != null) {
//sessionid = cookieval.substring(0, cookieval.indexOf(";"));
result = result.substring(0, result.length() - 1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}





PHP


$filename="image.jpg";
$xmlstr = file_get_contents('php://input');
$jpg = base64_decode($xmlstr);
$file = fopen($filename,"w");
fwrite($file,$jpg);
fclose($file);


你可能感兴趣的:(android)