1

I have been trying to create an Android camera activity to test how it works on the emulator, but I am not sure if I am doing things right.

I have added the permission to the manifest for the deprecated camera version, focus and front camera. And I have been looking up tutorials and learning the code.

  1. I have also tried to include a frame layout preview with some custom buttons, but I really don't know how to make the buttons layout overlay the frame.

  2. Do I need to use fragments?

  3. Also I should mention I have read about the new "camera2" and my interest to implement it to the same activity, but maybe that would be just too much for a simple test. What are your recommendations on this?

diyoda_
  • 5,274
  • 8
  • 57
  • 89
Matej Varga
  • 95
  • 2
  • 14
  • I see that you have several different questions in this questions, I suggest you to go for the Camera2 API. You can find sample code which uses Camea2 API [here](https://github.com/googlesamples/android-Camera2Basic). Read through the code and understand. But you will not understand the whole code within minutes, will take time – diyoda_ Aug 29 '15 at 08:06
  • Thank you for your suggestion. Will camera2 work with API under 21? I wanted to learn both, are they very different to each other or is there a way to make them both compatible? I would like to test this on my device and it's a Jellybean. – Matej Varga Aug 29 '15 at 08:08
  • Yes camera2 works v21 and up. But still the earlier API works fine and it is easy to use. But the new API is core complex, which means more flexible and more power for the developer. If you are targeting Jellybeans I think earlier version is the most compatible API. – diyoda_ Aug 29 '15 at 08:10
  • I see, I think camera would be more convenient for testing the camera. It's a real pity, I was getting very interested in the camera2 improvements. Is there a way to include them both in the manifest and being camera or camera 2 not required so any device would be able to recognize its version? You know what I mean, making the device search the camera as the developer tutorial says. – Matej Varga Aug 29 '15 at 08:15

2 Answers2

0

If your search for code, try the following sample code (I have also an answer at How to move image captured with the phone camera from one activity to an image view in another activity?, you can take a look).

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        mFileUri = Uri.fromFile(getOutputMediaFile(1));

        intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);

        // start the image capture Intent
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if (resultCode == RESULT_OK) {
            if (mFileUri != null) {
                // do something...
            }
        }            
    }

    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == 1) { // image
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
        } else if (type == 2) { // video
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

If using emulator to test, make sure camera supported like this:

enter image description here

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
0

I do not think that I can give you a exact answer to you question since it is very brad thing to answer. But I will try to give you some high-level guidance to your approaches.

Generally using Fragments is the best way to write an Android application and now it is the recommended way.

You can make another application to work on behalf of your application using Intents in Android. Which is you can start the Camera Application installed on your device to take the images on behalf of your application.

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

You can read more from here

But these camera APIs come into play if you want to write your own custom Camera API. Because there are scenarios where you actually want your own custom camera functionality for your special requirements of the application you are building.

So decide what is that you want to achieve. If you just want to get a photo and save it you can simply use Intent and request the camera application to take the photo for your application.

To get more in depth understanding on camera APIs you can start from here

Edit,

Yes you can actually do something like following

if (Build.VERSION.SDK_INT > 9) { 

and have different execution paths depending on the version.

diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • Thank you for your answer, as you mentioned, my point was to develop a custom camera for my test app, which means including my own UI with buttons, but taking as high quality pictures as the phone camera does. – Matej Varga Aug 30 '15 at 06:23