在分离按钮和imageview上发送2张图像

时间:2015-01-24 09:45:16

标签: java android base64

我是android开发的初学者 我有一个按钮,用于将图像加载到imageview,通过PHP上传到服务器,并说我想要另一个按钮和另一个图像视图,这样我就可以上传2张图像(以及它的路径)。
下面是我发送图像的代码。 是否使函数的副本成为一个好主意? 如果意见附带代码,我将非常感激。

这个是按钮的功能,并获取文件名及其路径

public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgPath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.imgView);
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgPath));
            // Get the Image's file name
            String fileNameSegments[] = imgPath.split("/");
            fileName = fileNameSegments[fileNameSegments.length - 1];
            }
            else{
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }
  }<br/><br/><br/>

,这是提交按钮上的上传功能。

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));

有什么意见吗?

1 个答案:

答案 0 :(得分:0)

首先是拳头。如果你有一个特定的问题(就像你标记为Android的那样),那么请问。这是一个通用的编程疑问。

第二:当然最好不要重复代码,至少在单个文件中。您可以创建一个上传到服务器并从路径中提取图像文件的功能,以便您可以单独使用。如果你是编程的新手,那么不要担心。你肯定会学到它。