Java 在分离的按钮和imageview上发送2个图像
我是android开发的初学者。 我有一个按钮,用于将图像加载到imageview,通过PHP上传到服务器,并说我需要另一个按钮和另一个imageview,这样我可以上传2个图像及其路径。 下面是我发送图像的代码。 复制函数是个好主意吗? 我非常感激,如果意见与代码一起提供。 这是一个用于按钮和获取文件名及其路径的函数 这是提交按钮上的上传功能Java 在分离的按钮和imageview上发送2个图像,java,android,base64,Java,Android,Base64,我是android开发的初学者。 我有一个按钮,用于将图像加载到imageview,通过PHP上传到服务器,并说我需要另一个按钮和另一个imageview,这样我可以上传2个图像及其路径。 下面是我发送图像的代码。 复制函数是个好主意吗? 我非常感激,如果意见与代码一起提供。 这是一个用于按钮和获取文件名及其路径的函数 这是提交按钮上的上传功能 buttonSubmit.setOnClickListener(new View.OnClickListener() { InputSt
buttonSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View view) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath,
options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
String address = "" + etAddress.getText().toString();
String zipCode = "" + etZip.getText().toString();
String identifier = "" + etIdentifier.getText().toString();
String stories = "" + etStories.getText().toString();
String year = "" + etYear.getText().toString();
String name = "" + etName.getText().toString();
String area = "" + etArea.getText().toString();
String bName = "" + etbName.getText().toString();
String usage = "" + etUsage.getText().toString();
String occupancy = "" + spOccupancy.getSelectedItem().toString();
String person = "" + spPerson.getSelectedItem().toString();
String soilType = "" + spSoilType.getSelectedItem().toString();
String falling = "" + spFallingHazard.getSelectedItem().toString();
String bType = "" + spinner.getSelectedItem().toString();
String basicScore = "" + basic.getText().toString();
String midrise = "" + midRise.getText().toString();
String highrise = "" + highRise.getText().toString();
String virregularity = "" + vIrregularity.getText().toString();
String pirregularity = "" + pIrregularity.getText().toString();
String precode = "" + preCode.getText().toString();
String pbenchmark = "" + postBenchmark.getText().toString();
String soiltypec = "" + sTypeC.getText().toString();
String soiltyped = "" + sTypeD.getText().toString();
String soiltypee = "" + sTypeE.getText().toString();
String finalscore = "" + finalScore.getText().toString();
String lati = "" + textViewNetLat.getText().toString();
String longi = "" + textViewNetLng.getText().toString();
String comments = "" + comment.getText().toString();
String eval = "" + spEval.getSelectedItem().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("address", address));
nameValuePairs.add(new BasicNameValuePair("zipCode", zipCode));
nameValuePairs.add(new BasicNameValuePair("identifier", identifier));
nameValuePairs.add(new BasicNameValuePair("stories", stories));
nameValuePairs.add(new BasicNameValuePair("year", year));
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("area", area));
nameValuePairs.add(new BasicNameValuePair("bName", bName));
nameValuePairs.add(new BasicNameValuePair("usage", usage));
nameValuePairs.add(new BasicNameValuePair("filename", fileName));
nameValuePairs.add(new BasicNameValuePair("image", encodedString));
nameValuePairs.add(new BasicNameValuePair("occupancy", occupancy));
nameValuePairs.add(new BasicNameValuePair("person", person));
nameValuePairs.add(new BasicNameValuePair("soilType", soilType));
nameValuePairs.add(new BasicNameValuePair("falling", falling));
nameValuePairs.add(new BasicNameValuePair("bType", bType));
nameValuePairs.add(new BasicNameValuePair("basicScore", basicScore));
nameValuePairs.add(new BasicNameValuePair("midrise", midrise));
nameValuePairs.add(new BasicNameValuePair("highrise", highrise));
nameValuePairs.add(new BasicNameValuePair("virregularity", virregularity));
nameValuePairs.add(new BasicNameValuePair("pirregularity", pirregularity));
nameValuePairs.add(new BasicNameValuePair("precode", precode));
nameValuePairs.add(new BasicNameValuePair("pbenchmark", pbenchmark));
nameValuePairs.add(new BasicNameValuePair("soiltypec", soiltypec));
nameValuePairs.add(new BasicNameValuePair("soiltyped", soiltyped));
nameValuePairs.add(new BasicNameValuePair("soiltypee", soiltypee));
nameValuePairs.add(new BasicNameValuePair("finalscore", finalscore));
nameValuePairs.add(new BasicNameValuePair("lati", lati));
nameValuePairs.add(new BasicNameValuePair("longi", longi));
nameValuePairs.add(new BasicNameValuePair("comments", comments));
nameValuePairs.add(new BasicNameValuePair("eval", eval));
有什么意见吗?第一件事。如果你有一个特定的问题,比如你已经标记为Android,这不是那么问。这是一个通用编程问题 第二:当然,最好不要重复代码,至少在单个文件中。您可以创建一个函数来上传到服务器中,并从path中提取图像文件,这样您就可以将两者分开。如果你是一个编程新手,那么别担心。你一定会学会的
buttonSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View view) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath,
options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
String address = "" + etAddress.getText().toString();
String zipCode = "" + etZip.getText().toString();
String identifier = "" + etIdentifier.getText().toString();
String stories = "" + etStories.getText().toString();
String year = "" + etYear.getText().toString();
String name = "" + etName.getText().toString();
String area = "" + etArea.getText().toString();
String bName = "" + etbName.getText().toString();
String usage = "" + etUsage.getText().toString();
String occupancy = "" + spOccupancy.getSelectedItem().toString();
String person = "" + spPerson.getSelectedItem().toString();
String soilType = "" + spSoilType.getSelectedItem().toString();
String falling = "" + spFallingHazard.getSelectedItem().toString();
String bType = "" + spinner.getSelectedItem().toString();
String basicScore = "" + basic.getText().toString();
String midrise = "" + midRise.getText().toString();
String highrise = "" + highRise.getText().toString();
String virregularity = "" + vIrregularity.getText().toString();
String pirregularity = "" + pIrregularity.getText().toString();
String precode = "" + preCode.getText().toString();
String pbenchmark = "" + postBenchmark.getText().toString();
String soiltypec = "" + sTypeC.getText().toString();
String soiltyped = "" + sTypeD.getText().toString();
String soiltypee = "" + sTypeE.getText().toString();
String finalscore = "" + finalScore.getText().toString();
String lati = "" + textViewNetLat.getText().toString();
String longi = "" + textViewNetLng.getText().toString();
String comments = "" + comment.getText().toString();
String eval = "" + spEval.getSelectedItem().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("address", address));
nameValuePairs.add(new BasicNameValuePair("zipCode", zipCode));
nameValuePairs.add(new BasicNameValuePair("identifier", identifier));
nameValuePairs.add(new BasicNameValuePair("stories", stories));
nameValuePairs.add(new BasicNameValuePair("year", year));
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("area", area));
nameValuePairs.add(new BasicNameValuePair("bName", bName));
nameValuePairs.add(new BasicNameValuePair("usage", usage));
nameValuePairs.add(new BasicNameValuePair("filename", fileName));
nameValuePairs.add(new BasicNameValuePair("image", encodedString));
nameValuePairs.add(new BasicNameValuePair("occupancy", occupancy));
nameValuePairs.add(new BasicNameValuePair("person", person));
nameValuePairs.add(new BasicNameValuePair("soilType", soilType));
nameValuePairs.add(new BasicNameValuePair("falling", falling));
nameValuePairs.add(new BasicNameValuePair("bType", bType));
nameValuePairs.add(new BasicNameValuePair("basicScore", basicScore));
nameValuePairs.add(new BasicNameValuePair("midrise", midrise));
nameValuePairs.add(new BasicNameValuePair("highrise", highrise));
nameValuePairs.add(new BasicNameValuePair("virregularity", virregularity));
nameValuePairs.add(new BasicNameValuePair("pirregularity", pirregularity));
nameValuePairs.add(new BasicNameValuePair("precode", precode));
nameValuePairs.add(new BasicNameValuePair("pbenchmark", pbenchmark));
nameValuePairs.add(new BasicNameValuePair("soiltypec", soiltypec));
nameValuePairs.add(new BasicNameValuePair("soiltyped", soiltyped));
nameValuePairs.add(new BasicNameValuePair("soiltypee", soiltypee));
nameValuePairs.add(new BasicNameValuePair("finalscore", finalscore));
nameValuePairs.add(new BasicNameValuePair("lati", lati));
nameValuePairs.add(new BasicNameValuePair("longi", longi));
nameValuePairs.add(new BasicNameValuePair("comments", comments));
nameValuePairs.add(new BasicNameValuePair("eval", eval));