I am trying to take a picture and view it on the screen with the following code:
public class MainActivity extends ActionBarActivity {
private static final int PICTURE_REQUEST_CODE = 100;
public Button camera, gallery;
private Uri fileUri;
private static File mediaFile;
ImageView image;
public String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Started Main Activity");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = (Button) findViewById(R.id.button1);
gallery = (Button) findViewById(R.id.button2);
createListeners();
}
private static Uri getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Mustache");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
return Uri.fromFile(mediaFile);
}
public void launchCamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFile(); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, PICTURE_REQUEST_CODE);
}
public void launchGallery(){
Intent gallery = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
fileUri = getOutputMediaFile(); // create a file to save the image
gallery.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(gallery, PICTURE_REQUEST_CODE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println(String.valueOf(requestCode));
System.out.println(String.valueOf(resultCode));
System.out.println(data);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(mediaFile);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
public static final int MEDIA_TYPE_VIDEO = 2;
private void createListeners() {
System.out.println("Started CreateListeners for Main Activity");
camera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
launchCamera();
}
});
gallery.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
launchGallery();
}
});
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context="com.example.mustache.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="@string/main_camera_button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="@string/main_gallery_button" />
</LinearLayout>
However, the app takes a photo and saves it to my gallery, but then returns to the main screen without doing anything. How do I make it display the photo?