一旦我在ImageView中有图像,我怎样才能以最简单的方式将图像发送到Web服务器?
我使用以下方式从图库中获取了图像:
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服务器(最简单的方式),而不是
答案 0 :(得分:3)
我没有用PHP做过这个,但是用.NET使用base64字符串发送了图像。
将您的图像转换为base64并在您的服务器上发送此字符串。您的服务器会将此base64转换为原始图像
将图像转换为byte []尝试以下代码
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();
}
}
答案 1 :(得分:0)
这是此网址的代码(我在评论中指出 - How do I send a file in Android from a mobile device to server using http?):
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
}
我想这很简单。而不是File
和FileInputStream(file)
我认为您可以使用类型为stream
的{{1}} - 但我不确定......