-3

Ich muss ein Bild von der Kamera zu erfassen, und ich muss es von innerhalb eines Fragments tun. Mein Problem ist, dass OnActivityResult nie aufgerufen wird. Unten ist mein Code:Capture Bild von Fragment

private void dispatchTakePictureIntent(){ 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 
    } 



@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { 
    Bundle extras = data.getExtras(); 
    Bitmap imageBitmap = (Bitmap) extras.get("data"); 
    im.setImageBitmap(imageBitmap); 
    } 
    Log.e(TAG,"hello"); 
    super.onActivityResult(requestCode, resultCode, data); 
} 

Ich habe auf diesem Forum einige Threads hier nachgeschlagen, aber keiner von ihnen für mich arbeiten.

+0

Mögliches Duplikat von [onActivityResult For Fragment] (http://stackoverflow.com/questions/20038880/onactivityresult-for-fragment) – Pankaj

Antwort

0

müssen Sie auch super.onActivityResult(requestCode, resultCode, data); in onActivityResult übergeben.

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { 
    Bundle extras = data.getExtras(); 
    Bitmap imageBitmap = (Bitmap) extras.get("data"); 
    im.setImageBitmap(imageBitmap); 
    } 
    Log.e(TAG,"hello"); 
    super.onActivityResult(requestCode, resultCode, data); 
} 
0

Sie können wie unten etwas tun es in Fragment zu nennen setzen Sie zuerst den Code in Ihre onActivityResult() Methode von activity, von dem Sie fragment erstellt haben, wie folgt:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Fragment fragment = getSupportFragmentManager().findFragmentByTag("Your Fragment Tag"); 
     fragment.onActivityResult(requestCode, resultCode, data); 
    } 

Jetzt brauchen Sie nur in Ihrem Fragment setzen onActivityResult() Verfahren, und es wäre

1

genannt werden ich auf diese Weise versucht, und das war für mich

public class CameraPic extends Fragment { 

    private static final int CAPTURE_PICCODE = 989; 
    Button button; 
    ImageView imageView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     final View rootView = inflater.inflate(R.layout.cameralayout, 
       container, false); 

     button = (Button) rootView.findViewById(R.id.button1); 
     imageView = (ImageView) rootView.findViewById(R.id.imageview1); 

     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, 
         CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

      } 
     }); 

     return rootView; 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      if (resultCode == Activity.RESULT_OK) { 

       Bitmap bmp = (Bitmap) data.getExtras().get("data"); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

       bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
       byte[] byteArray = stream.toByteArray(); 



       Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, 
         byteArray.length); 

       imageView.setImageBitmap(bitmap); 

      } 
     }   
    }  
}