2015-01-24 2 views
0

im은 안드로이드 개발에 대한 초보 개발자입니다.
저는 PHP를 통해 서버에 업로드하기 위해 이미지 뷰에 이미지를로드하기위한 버튼이 있습니다. 다른 버튼과 다른 이미지 뷰를 원한다고 말하면 2 개의 이미지 (그리고 그것의 경로)를 업로드 할 수 있습니다. 아래의
은 이미지를 보내는 코드입니다. 함수의 복사본을 좋은 생각으로 만드시겠습니까? 코드에 의견이 있으면 매우 감사하겠습니다.

이 하나의 버튼 및 파일 이름을 얻기위한 기능이며 경로
분리 된 버튼 및 이미지보기에 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)); 

의견이 있으십니까?

답변

0

주먹 우선. 특정 문제가있는 경우 (안드로이드로 태그를 지정한 것과 같음) 물어보십시오. 이것은 일반적인 프로그래밍에 대한 의심입니다.

둘째 : 적어도 하나의 파일에서 코드를 반복하지 않는 것이 좋습니다. 서버에 업로드하고 경로에서 이미지 파일을 추출하는 함수를 작성하여 둘 다 별도로 수행 할 수 있습니다. 프로그래밍에 초보자라면 걱정하지 마십시오. 당신은 확실히 그것을 배울 것입니다.

관련 문제