Skip to content

Commit

Permalink
Create an Intent with an action to invoke the camera app
Browse files Browse the repository at this point in the history
Store the treasures photo path and the tobe taken photos path in fields
Store the grid list adapter in a field so that we can use it later
  • Loading branch information
alexmetcalfe committed Mar 13, 2014
1 parent 2499eac commit 205897d
Showing 1 changed file with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@


import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -12,17 +17,29 @@

import com.thoughtworks.androidbootcamp.R;
import com.thoughtworks.androidbootcamp.controller.adapter.TreasureListAdapter;
import com.thoughtworks.androidbootcamp.util.FileUtils;
import com.thoughtworks.androidbootcamp.util.TreasureLoader;

import java.io.File;
import java.io.IOException;


/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class TreasureListFragment extends Fragment {

private static final String TAG = "TreasureListFragment";
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

TreasureLoader treasureLoader;

private String mCurrentPhotoPath;
private String mSelectedTreasurePath;

private TreasureListAdapter mTreasureListAdapter;

public TreasureListFragment() {
// Required empty public constructor
}
Expand All @@ -45,14 +62,41 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
GridView gridView = (GridView) view.findViewById(R.id.treasure_list);
gridView.setAdapter(new TreasureListAdapter(getActivity(), treasureLoader.getSampleImagePaths()));
mTreasureListAdapter = new TreasureListAdapter(getActivity(), treasureLoader.getSampleImagePaths());
gridView.setAdapter(mTreasureListAdapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

mSelectedTreasurePath = (String) mTreasureListAdapter.getItem(position);
takePhoto();
}
});

}

public void takePhoto() {
// Create a new intent for taking a photo
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// Create the File where the photo should go
try {
File photoFile = FileUtils.getExternalPublicFile(Environment.DIRECTORY_PICTURES,
getString(R.string.app_name));

// store the path so we know where it is later
mCurrentPhotoPath = "file:" + photoFile.getAbsolutePath();

// tell camera app when to put the photo
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

if(intent.resolveActivity(getActivity().getPackageManager()) != null) {
// ask to use the camera
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

} catch (IOException ex) {
Log.e(TAG, "Error opening file", ex);
}
}
}

0 comments on commit 205897d

Please sign in to comment.