如何制作图像附加按钮 [英] How to make image attatchment button

查看:63
本文介绍了如何制作图像附加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作附件活动...意味着我必须创建一个活动,其中我必须为用户建议提供编辑文本框,以及用于添加图像的图像按钮,如果有必要与他的建议相关...然后将此数据发送到服务器...

i创建了一个编辑文本并将数据发送到网络服务器..但我不知道图片上传..



我尝试过:



how to make an attachment activity... Means i have to create an activity in which i have to give a edit text box for user suggestion and an image button for adding an image if necessary related to his suggestion.. and then send this data to server...
i have created an edit text and send data to web server.. but i have no idea about image upload..

What I have tried:

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("");
        }
    }

推荐答案

参见 android上传图片通过http - Google搜索 [ ^ ]。


这篇关于如何制作图像附加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆