2013-10-25 4 views
0

두 개의 버튼과 두 개의 이미지 뷰가 있습니다. 첫 번째 버튼을 클릭하면 열리고 이미지가 업로드됩니다. 두 번째 버튼 클릭 및 이미지로드 작업은 어떻게해야합니까? 내 코드는 다음과 같습니다안드로이드의 여러 버튼에 여러 이미지를 업로드하는 방법

private static final int PICK_IMAGE = 1; 
upload1=(Button)findViewById(R.id.uploadimage1); 
upload2=(Button)findViewById(R.id.uploadimage2); 
imgView1=(ImageView)findViewById(R.id.image1); 
imgView2=(ImageView)findViewById(R.id.image2); 
upload1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      try { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(
         Intent.createChooser(intent, "Select Picture"), 
         PICK_IMAGE); 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), 
         "No image found", Toast.LENGTH_LONG).show(); 
       Log.e(e.getClass().getName(), e.getMessage(), e); 

     } 
     } 
    }); 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
     case PICK_IMAGE: 
      if (resultCode == Activity.RESULT_OK) { 
       Uri selectedImageUri = data.getData(); 
       String filePath = null; 

       try { 
        // OI FILE Manager 
        String filemanagerstring = selectedImageUri.getPath(); 

        // MEDIA GALLERY 
        String selectedImagePath = getPath(selectedImageUri); 

        if (selectedImagePath != null) { 
         filePath = selectedImagePath; 
        } else if (filemanagerstring != null) { 
         filePath = filemanagerstring; 
        } else { 
         Toast.makeText(getApplicationContext(), "Unknown path", 
           Toast.LENGTH_LONG).show(); 
         Log.e("Bitmap", "Unknown path"); 
        } 

        if (filePath != null) { 
         decodeFile(filePath); 
        } else { 
         bitmap = null; 
        } 
       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), "Internal error", 
          Toast.LENGTH_LONG).show(); 
        Log.e(e.getClass().getName(), e.getMessage(), e); 
       } 
      } 
      break; 
     default: 
     } 
    } 
    public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if (cursor != null) { 
      // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
      // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
      int column_index = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } else 
      return null; 
    } 
    public void decodeFile(String filePath) { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filePath, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 1024; 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     bitmap = BitmapFactory.decodeFile(filePath, o2); 
        //first image i have uploaded by using first button 
     imgView1.setImageBitmap(bitmap); 


    } 

이제 제 질문은 두 번째 버튼의 두 번째 이미지 업로드 방법입니다. 이 문제를 해결하도록 도와주세요. 미리 감사드립니다 upload2에 대한

답변

0
private static final int PICK_IMAGE = 1; 
private static final int PICK_IMAGE_2 = 2; 

설정을 클릭 리스너. 당신이하여 onActivityResult의 데이터를 사용하는 방법을 알 수 있도록 그런 다음 당신은 단순히 당신이 첫 번째 단추를 위해 한 일을 반복하지만, 다른 요청 코드와 함께 할 수 onActivityResult()

case PICK_IMAGE : 
imgView1.setImageBitmap(bitmap); 
break; 
CASE PICK_IMAGE : 
imgView2.setImageBitmap(bitmap); 
break; 
+0

알아 두십시오. @AndroidWarrior –

+0

도와 주셔서 감사합니다 :) – CodeWarrior

0

에서 마지막으로

startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
          PICK_IMAGE_2); 

OnClick()에 :

public void onClick(View v) { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_2); 
} 
관련 문제