如何将ImageView中的图像发送到php服务器?
一旦我在ImageView中有了一个图像,我如何才能以最简单的方式将图像发送到web服务器 我从图库中获得了以下图像:如何将ImageView中的图像发送到php服务器?,php,android,android-camera,Php,Android,Android Camera,一旦我在ImageView中有了一个图像,我如何才能以最简单的方式将图像发送到web服务器 我从图库中获得了以下图像: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) try { //
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
我已将该图像设置为imageView。我这样做是为了向上传人显示图像预览。现在,如何将该图像上传到web服务器(最好的、最简单的方式)谢谢你我没有用PHP完成这项工作,而是使用base64字符串发送带有.NET的图像 将图像转换为base64并在服务器上发送此字符串。您的服务器将此base64转换为原始映像 要将图像转换为字节[],请尝试以下代码
private void setPhoto(Bitmap bitmapm) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmapm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String imagebase64string = Base64.encodeToString(byteArrayImage,Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
}
这是来自此URL的代码(我在评论中指出-):
我想这很简单。而不是
文件
和文件输入流(文件)
我认为您可以使用输入流类型的流
-但我不确定…这可能与以下问题重复:使用了可绘制的…但是如何将位图作为字符串编码签出编辑的答案,如果您觉得答案有用,则接受答案
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}