gpt4 book ai didi

java - 如何使用 post 方法将简单的图像从 Android 发布到服务器

转载 作者:行者123 更新时间:2023-12-01 17:18:54 25 4
gpt4 key购买 nike

Android 中如何发布图像

  • 我是新手,我正在寻找有关如何操作的分步说明图像的发布在 android 中进行
  • 互联网上有什么好的信息来源可以学习吗这个
  • 我想要学习的就是从 imageview 获取图像并将其发布到服务器
<小时/>

我尝试了什么

我已经学会将字符串发送到服务器

<小时/>

这是我如何将 srtings 发布到服务器

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:clickable="false"
android:src="@drawable/ic_launcher" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView1"
android:text="Click to upload Image"
android:textSize="15dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/NAME_EDIT_TEXT_ID"
android:layout_alignParentLeft="true"
android:clickable="false"
android:text="NAME"
android:textSize="20dp"
android:textStyle="bold" />

<EditText
android:id="@+id/NAME_EDIT_TEXT_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/CITY_EDIT_TEXT_ID"
android:layout_alignRight="@+id/button1"
android:layout_marginBottom="30dp"
android:ems="10" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/CITY_EDIT_TEXT_ID"
android:layout_alignLeft="@+id/textView2"
android:clickable="false"
android:text="CITY"
android:textSize="20dp"
android:textStyle="bold" />

<EditText
android:id="@+id/CITY_EDIT_TEXT_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/NAME_EDIT_TEXT_ID"
android:layout_centerVertical="true"
android:ems="10" />

<Button
android:id="@+id/SUBMIT_BUTTON_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_marginBottom="47dp"
android:text="SUBMIT" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

Button submit;
EditText name, City;
ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);
name = (EditText) findViewById(R.id.NAME_EDIT_TEXT_ID);
City = (EditText) findViewById(R.id.CITY_EDIT_TEXT_ID);

submit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();


}
});
}


public void postData() {
// Create a new HttpClient and Post Header

// You can use NameValuePair for add data to post server and yes you can
// also append your desire data which you want to post server.

// Like:
// yourserver_url+"name="+name.getText().toString()+"city="+City.getText().toString()

String newurl = "?" + "Key=" + name.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://My-URL"+newurl);

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Name", name.getText()
.toString()));
nameValuePairs.add(new BasicNameValuePair("city", City.getText()
.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.v("Response", response.toString());

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

public class MainTest extends AsyncTask<String, Integer, String> {

@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected String doInBackground(String... params) {

postData();

return null;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub

super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();

}

}

}
<小时/>

现在我如何修改代码以便从 imageview 获取图像并将其发送到服务器?

  • 任何指导都会有帮助
  • 我是新手,所以请简单地回答

谢谢

最佳答案

以下是一个场景,说明图像如何从一种格式转换为另一种格式,最后返回原始格式。

enter image description here

尝试下面的代码

Android端

private void uploadToServer(byte[] data) {
Bitmap bitmapOrg = BitmapFactory.decodeByteArray(data, 0, data.length);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
final ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
Thread t = new Thread() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.yoururl.com");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
// HttpEntity entity = response.getEntity();

// is = entity.getContent();
// String the_string_response =
// convertResponseToString(response);
// Log.e("log_tag", "Image Uploaded "+the_string_response);
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
}
};

}

服务器端

<?php

$base=$_REQUEST['image'];

echo $base;

// base64 encoded utf-8 string

$binary=base64_decode($base);

// binary, utf-8 bytes

header('Content-Type: bitmap; charset=utf-8');

// print($binary);

//$theFile = base64_decode($image_data);

$file = fopen('test.jpg', 'wb');

fwrite($file, $binary);

fclose($file);

echo '<img src=test.jpg>';

?>

Complete Tutorial

关于java - 如何使用 post 方法将简单的图像从 Android 发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20137717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com