2016-06-17 19 views
0

你知道如何在一个活动中处理两个onActivityResult()吗?两个onActivityResults在一个活动

我需要使用我的相机并在一项活动中搜索我的照片。

public class MainActivity extends AppCompatActivity { 

     public static final int REQUEST_CAPTURE = 1; 

     Button button_Vyber_Fotku, button_Fotak; 

     ImageView imageView_VyberFotku, imageView_Fotak; 
     private static final int PICK_IMAGE = 100; 
     Uri imageUri_vybrana; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      imageView_VyberFotku = (ImageView) findViewById(R.id.imageView_VyberFotku); 
      button_Vyber_Fotku = (Button) findViewById(R.id.button_Vyber_Fotku); 

      imageView_Fotak = (ImageView) findViewById(R.id.imageView_Fotak); 
      button_Fotak = (Button) findViewById(R.id.button_fotak); 

      if (!hasCamera()) 
      { 
       button_Fotak.setEnabled(false); 
      } 

     } 

      public void Vyber_fotku_clicked(View v) 
      { 
       openGallery(); 
      } 
     private void openGallery() 
     { 
      Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
      startActivityForResult(gallery, PICK_IMAGE); 
     } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
      if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){ 
       imageUri_vybrana = data.getData(); 
       imageView_VyberFotku.setImageURI(imageUri_vybrana); 
      } 
     } 

     public boolean hasCamera() 
     { 
      return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY); 
     } 

     public void PouzijFotakClicked(View v) 
     { 
      Intent vyfot = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(vyfot , REQUEST_CAPTURE); 
     } 


     @Override 
     protected void onActivityResult(int requestCode1, int resultCode1, Intent data1) 
     { if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK) 
      { 
       Bundle extras = data1.getExtras(); 
       Bitmap photo = (Bitmap) extras.get("data1"); 
       imageView_Fotak.setImageBitmap(photo); 
      } 
     } 

    } 
+2

你有一个活动使用一个onActivityResults,你可以使用2个不同的请求代码来处理情况 –

+0

你**在同一个类不能覆盖任何方法的两倍。**您可以在活动覆盖onActivityResult和使用不同的请求代码来区分相机图片和图片pic – Apurva

+0

[在一个onActivityResult()中处理来自多个活动的数据的重复?](http://stackoverflow.com/questions/15001746/handle-data-from-several- activities-in-one-onactivityresult) – Sufian

回答

0

入住这 它为我

private static final int CAMERA_ = 999; 
private static final int GALLERY_ = 888; 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_) { 
      if (resultCode == RESULT_OK) { 

       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 8; 
       UtilsClass.mBitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
         options); 
       imageView.setImageBitmap(UtilsClass.mBitmap); 

      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 

     }else if (requestCode == GALLERY_) { 
      if (resultCode == RESULT_OK) { 
       Uri selectedImage = data.getData(); 
       String[] filePath = {MediaStore.Images.Media.DATA}; 
       Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null); 
       c.moveToFirst(); 
       int columnIndex = c.getColumnIndex(filePath[0]); 
       String picturePath = c.getString(columnIndex); 
       c.close(); 
       UtilsClass.mBitmap = (BitmapFactory.decodeFile(picturePath)); 
       imageView.setImageBitmap(UtilsClass.mBitmap); 


      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
+0

不要发布**仅链接的答案。**在你的答案中分享有用的代码,以便将来如果链接被破坏,答案不会失效。 –

+0

好的谢谢:) – Sach

2

而不是为onActivityResults使用单一的方法两种不同的方法,并根据他们的请求代码区分它们。

@Override 
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1){ 
    if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK){ 
      Bundle extras = data1.getExtras(); 
      Bitmap photo = (Bitmap) extras.get("data1"); 
      imageView_Fotak.setImageBitmap(photo); 
    } 
    else if (resultCode1 == RESULT_OK && requestCode1 == PICK_IMAGE){ 
     imageUri_vybrana = data1.getData(); 
     imageView_VyberFotku.setImageURI(imageUri_vybrana); 
    } 
} 

注:你不能有两个申报单重写方法。

0
// define two variable camera and pick_image of int type pass value of request code of desired out put in activity onResult 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){ 
    //first one to pick image 
    //do somthing 
    }else if(resultCode == RESULT_OK && requestCode == Camera){ 
//use to take image from camera response 
} 
    }