1- 获取 base64 编码的图像 URI
String imageData= canvas2.toDataUrl();
2- 通过 RPC 调用将图像数据发送到服务器端
jdbc.saveImage(imageData,callback);
3- 向您的 Web 服务器 API 发出 HTTP Post 请求
URL url = new URL("http://myserver.com/my_images_folder/save_image.php");
URLConnection conn = url.openConnection();
conn.setReadTimeout(15000); //set a large time out since we're saving images
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response which contains the image file name
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
resa+=line;
}
wr.close();
System.out.println("close1");
rd.close();
System.out.println("Received: "+line);
- 服务器端(您的 Web 服务器 -php API):
4-将图像保存到文件服务器并返回图像文件名
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
//using a timestamp to create unique file names
//you can pass file name in params if you like instead
$fileName='User_Images_'.time().'.png';
// Remove the headers (data:,) part.
$filteredData=substr($imageData, strpos($imageData, ",")+1);
// Need to decode base64 encoded image
$unencodedData=base64_decode($filteredData);
$fp = fopen( $fileName, 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
$fileName2='http://myserver.com/my_images_folder/'.$fileName;
//return the file name
echo($fileName);
}else{
echo('no data posted');
}
现在我有了文件的硬永久链接,我可以将它嵌入到电子邮件中并用它做其他事情。请参阅下面的参考 3 以了解内联嵌入(这需要文件或 URL,现在我们的网络服务器上有图像的硬 URL,我们可以通过电子邮件将其发送出去)