Search code examples
javaandroidadt

I want to open the android camera without saving the picture to gallery


Screenshot

I want the picture to go straight to the ImageView without saving it to gallery if possible. As shown in the screenshot, it will ask to save everytime and will save straight to the gallery. Can this be achieved, or will I have to make my own ImageView camera?

public class Main extends Activity {

ImageView ivPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ivPhoto = (ImageView) findViewById(R.id.ivPic);
}

public void TakePhoto(View v){
    Intent camIntent = new      Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camIntent,0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==0){
        Bitmap camImage = (Bitmap) data.getExtras().get("data");
        ivPhoto.setImageBitmap(camImage);
    }
}

Solution

  • From my understanding you don't want this to be showing up by any media scanner, like the gallery application. What you should actually do is not store it in a root directory like pictures or sdcard, but store it in your applications data folder in the sdcard in Android/data/package/.

    You can get this using: http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

    File myFilesDir = getExternalFilesDir(null);
    

    OR

    File myFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    

    Note that it will only work on API versions 8 or above.

    If you don't want to use the function you can simply just use:

    File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + packageName + "/files");
    myFilesDir.mkdirs();