Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture an image and store it with the native Android Camera

I am having a problem capturing an image and storing it from the native camera app. Here is a sample of some of my code.

_path = Environment.getExternalStorageDirectory() + "make_machine_example.jpg"; File file = new File( _path ); Uri outputFileUri = Uri.fromFile( file );  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE ); intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );  startActivityForResult( intent, 0 ); 

After the picture has been taken and I'm returned back to my original Activity, When I navigate to my sd card via Android DDMS File Explorer the picture is not there. Anyone know why this is not being saved?

like image 811
ninjasense Avatar asked Aug 09 '10 17:08

ninjasense


People also ask

How do you get an image from a camera or a gallery and save it in Android?

Run the application on an Android phone. Selecting "Take photo" will open your camera. Finally, the image clicked will be displayed in the ImageView. Selecting "Choose from Gallery" will open your gallery (note that the image captured earlier has been added to the phone gallery).

Does Image Capture work with Android?

Open Image Capture. In the sidebar, click on your Android device. Choose the folder where you want to save your pictures using the drop-down menu. Then, select the images you want to transfer and click Download.


2 Answers

Here was the final product in case anyone is still visiting this thread:

public class CameraCapture extends Activity {      protected boolean _taken = true;     File sdImageMainDirectory;      protected static final String PHOTO_TAKEN = "photo_taken";      @Override     public void onCreate(Bundle savedInstanceState) {          try {              super.onCreate(savedInstanceState);                              File root = new File(Environment                             .getExternalStorageDirectory()                             + File.separator + "myDir" + File.separator);                     root.mkdirs();                     sdImageMainDirectory = new File(root, "myPicName");                   startCameraActivity();             }         } catch (Exception e) {             finish();             Toast.makeText(this, "Error occured. Please try again later.",                     Toast.LENGTH_SHORT).show();         }      }      protected void startCameraActivity() {          Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);          Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");         intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);          startActivityForResult(intent, 0);     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         switch (resultCode) {         case 0:             finish();             break;          case -1:              try {                 StoreImage(this, Uri.parse(data.toURI()),                         sdImageMainDirectory);             } catch (Exception e) {                 e.printStackTrace();             }              finish();             startActivity(new Intent(CameraCapture.this, Home.class));          }      }      @Override     protected void onRestoreInstanceState(Bundle savedInstanceState) {         if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {             _taken = true;         }     }      @Override     protected void onSaveInstanceState(Bundle outState) {         outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);     }          public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {         Bitmap bm = null;         try {             bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);             FileOutputStream out = new FileOutputStream(imageDir);             bm.compress(Bitmap.CompressFormat.JPEG, 100, out);             bm.recycle();         } catch (FileNotFoundException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } catch (Exception e) {             e.printStackTrace();         }      }  } 
like image 74
ninjasense Avatar answered Sep 29 '22 14:09

ninjasense


Have you checked what the output of Environment.getExternalStorageDirectory() is, because if it does not contain a trailing file seperator (/) then your image will end up in a directory that does not reside on the SDcard such as:

 /mnt/sdcardmake_machine_example.jpg 

When what you really want is:

 /mnt/sdcard/make_machine_example.jpg 

Try this code instead:

 _path = Environment.getExternalStorageDirectory() + File.separator +  "make_machine_example.jpg"; 
like image 27
stealthcopter Avatar answered Sep 29 '22 13:09

stealthcopter