0

I am developing an android application, and I need to enable it to take photos, I have read many similar questions here and also many links on the web, but it seems there are so many ways to do this, in addition I am writing most part of my codes inside a Fragment so none of those links was very helpful for me

This is my code

public class MyActivity extends ActionBarActivity {
 /*
  some codes here
 */  
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MyFragment())
                .commit();
    }
}

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}


private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
    imageFileName, 
    ".jpg",         
    storageDir     
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
  }

btw I copied and pasted the onActivityResult and createImageFile codes from this link http://developer.android.com/training/camera/photobasics.html

And inside my MyFragment class I have

    public static class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       rootView = inflater.inflate(R.layout.my_activity, container, false);

       cameraButton = (ImageButton)rootView.findViewById(R.id.cameraButton);

        cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new     Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

            }
        });

Question Now I have no idea how should I invoke those methods createImageFileand OnActivityResult from MyActivity inside MyFragment to get them working together, in other words how to have access to stored image inside fragment.

please also let me know if I have to provide more details about my code

Siavosh
  • 2,314
  • 4
  • 26
  • 50

2 Answers2

1
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

write this code on MyFragment insted of Activity

Dedaniya HirenKumar
  • 2,980
  • 2
  • 22
  • 25
0

Add this class to your fragment and let the fragment handle the result.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
}
Xjasz
  • 1,238
  • 1
  • 9
  • 21