enter image description here如何制作附件活动...意味着我必须创建一个活动,其中我必须为用户建议提供编辑文本框,以及用于添加图像的图像按钮,如果有必要与他的相关建议..然后将此数据发送到服务器......
我创建了一个编辑文本并将数据发送到网络服务器..但我不知道图片上传..
我的MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
EditText ed1;
Button submit;
Button cancel;
Button btnselectphoto;
String toBeSent;
String imagePath=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.edit1);
submit = (Button) findViewById(R.id.submit);
cancel = (Button) findViewById(R.id.cancel);
submit.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.submit) {
// TODO Auto-generated method stub
if (ed1.getText().toString().length() < 1) {
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG)
.show();
} else {
toBeSent = ed1.getText().toString();
new MyAsyncTask(MainActivity.this, toBeSent,imagePath).execute();
}
}
else if (v.getId()==R.id.cancel) {
Toast.makeText(MainActivity.this, "thank you for comming", Toast.LENGTH_SHORT).show();
finish();
}
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private String data;
private InputStream is;
private String response;
private String imagePath;
public MyAsyncTask(Context context, String data,String imagePath) {
this.context = context;
this.data = data;
this.imagePath=imagePath;
}
@SuppressWarnings("deprecation")
@Override
protected Void doInBackground(Void... params) {
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs
.add(new BasicNameValuePair("dataToBeSent", data));
nameValuePairs.add(new BasicNameValuePair("image_path",imagePath));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.example.com/municipal/DummyTestFolder/insertQuery.php");// //URL will Come Here
UrlEncodedFormEntity ent= new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
httpPost.setEntity(ent);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
int statusCode=httpResponse.getStatusLine().getStatusCode();
Log.e("Status Code<><><>", ""+statusCode);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response = sb.toString();
Log.v("Login webservice response", "Login response == "
+ response);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if((response!=null)){
Toast.makeText(context, "Successfully Uploaded Comment", Toast.LENGTH_LONG).show();
ed1.setText("");
}
}
答案 0 :(得分:0)
使用此代码进行图片上传
public class UploadImage extends AsyncTask<Void, Void, Void> {
BufferedReader reader = null;
String data = Utils.PROFILE_UPLOAD_URL;
String text = "";
String fileName = sourceFile.substring(sourceFile.lastIndexOf("/") + 1);
ProgressDialog pd;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(ProfileDetails.this, R.style.MyTheme);
pd.setCancelable(false);
pd.setProgressStyle(android.R.style.Widget_ProgressBar_Large);
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(Utils.PROFILE_UPLOAD_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
Log.i("RES", text.toString());
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
String res = "";
try {
JSONObject jobj = new JSONObject(text);
res = jobj.getString("status_txt");
if (res.equalsIgnoreCase("ok")) {
String responce = jobj.getJSONObject("data").getString("thumb_url");
editor.putString(Utils.THUMB_URL, responce);
editor.commit();
Log.i("RESPONCE123", responce);
pd.dismiss();
} else {
}
} catch (JSONException jerror) {
}
}
}