1

I am working on adding a camera to my application, I have basically decided to go with adding an intent to it, and using the built in camera. The intent works, it calls it just fine, but when it goes to save it gets weird. according to Eclipse the photos are saving to the SD card, I can see them and pull them off when i go under FileExplorer. However, when I actually go to and explore the SD card, the files are not there.

public class C4Main extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private Camera cam;
private SurfaceHolder sh;
private Uri fileUri;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("testing","before intent");
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(1); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    Log.d("testing","fileUri: "+fileUri);
    // start the image capture Intent
    startActivityForResult(intent, 100);
    Log.d("testing","after startactivity");
}
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == 1){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
        Log.d("testing loop","Filepath: "+mediaFile.getPath());
    } else if(type == 2) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}
}

that is the code that I am using, copied from developer.google. And as I said according to FileExplorer in Eclipse it is saving to the designated path, but it just is not on my SD card. My hardware is ASUS Transformer prime running android 4.0.3. Tha manifest is set-up with permission for both external writing and camera use.

Any help would be greatly appreciated.

Paul Robert Carlson
  • 169
  • 1
  • 2
  • 14
  • Ok, problem still not solved, but after I restarted the tablet, the pictures did show up in their correct file. However, if I have to restart it every time I take pictures, it is going to get very annoying. – Paul Robert Carlson Mar 17 '12 at 05:38
  • Which file explorer are you using in your android device? – user370305 Mar 17 '12 at 05:52
  • It must be a hardware glitch. If I use File Manager on the device the pictures show up like they should right away. However, if use the built in gallery they do not show up; which also happens if I use File Explorer on my actual computer. Thanks to user370305- for mentioning in device, for that made me think of using file manager on my actual device instead of just on the computer. – Paul Robert Carlson Mar 17 '12 at 06:02

2 Answers2

2

I'm using this intent for using built-in camera. It automatically stores the image in SD card.

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    flagImage=1;
                    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
Neeku
  • 3,646
  • 8
  • 33
  • 43
ASP
  • 1,974
  • 3
  • 18
  • 30
0
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
        if (requestCode == CAMERA_REQUEST) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
Cursor c1 = cr.query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null,
                    null, p1[1] + " DESC");
if (c1.moveToFirst()) {
                String uristringpic = "content://media/external/images/media/"
                        + c1.getInt(0);
                Uri newuri = Uri.parse(uristringpic);
                // Log.i("TAG", "newuri "+newuri);
                String snapName = getRealPathFromURI(newuri);

                Uri u = Uri.parse(snapName);

                File f = new File("" + u);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
                photo.compress(CompressFormat.PNG, 0 /* ignored for PNG */,
                        bos);
                byte[] bitmapdata = bos.toByteArray();
// Storing Image in new folder
                StoreByteImage(mContext, bitmapdata, 100, fileName);
Vinit ...
  • 1,409
  • 10
  • 37
  • 66