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 createImageFile
and 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