Android Tutorial Intent Based Camera

Discussion in 'Tutorials & Examples' started by Erel, Jul 21, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This example uses an intent to take a picture with the default camera app.

    It is based on this tutorial:

    It is simple to use and doesn't require any permission (*).

    It does rely on the default camera app to work properly and save the image file in the path passed in the intent.
    As a fallback, if the image was not saved it tries to get the thumbnail from the intent returned.

    Don't miss the manifest editor code that is required for sharing file uris.

    Attached Files:

    Last edited: Feb 17, 2019
  2. Phayao

    Phayao Member Licensed User

    this is great, so no need to use a camera lib.
    Unfortunately i go the error :

    Error parsing program.
    Error description: Unknown type: runtimepermissions
    Are you missing a library reference?
    Occurred on line: 9 (Starter)
    Public rp As RuntimePermissions

    I did the changes in the manifest editor.
    Can it be that we need B4A v.6 (i have v.5.80) ?

    Thank you,
    Rockefeller Goldman likes this.
  3. asales

    asales Well-Known Member Licensed User

    Works fine in B4A v.6.
    Rockefeller Goldman likes this.
  4. DonManfred

    DonManfred Expert Licensed User

    yes you need v6
    Rockefeller Goldman and Phayao like this.
  5. Douglas Farias

    Douglas Farias Expert Licensed User

    this code
    String mCurrentPhotoPath;

    private File createImageFile() throws IOException {
        // Create an 
    image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
    ".jpg",         /* suffix */
            storageDir      /* directory */

        // Save a 
    filepath for use with ACTION_VIEW intents
        mCurrentPhotoPath = 
    "file:" + image.getAbsolutePath();
    return image;
    its not important on this lib?

    like ezcamra lib and intent camera codes posted on forum, sometimes the camera ignore the extra output and save the image with another name, in the case of your example.
    Private const tempImageFile As String = "tempimage.jpg"
    will be ignored like posted here

    and will be saved with random name file
    /mnt/sdcard/DCIM/Camera/2012-11-02 13.58.38.jpg (random file name!)

    using the code like google show "mCurrentPhotoPath" will fix this bug on some devices?

    here is a similar problem

    Converting the google code to b4a will result in this right?

    Private Sub GetFileName As String
    DateTime.TimeFormat = "yyyyMMdd_HHmmss"
    Private timestamps As String = DateTime.Time(DateTime.Now)
    Private imageFileName As String = "JPEG_" & timestamps & "_" &".jpg"
    Return imageFileName
    End Sub
    ps: I do not mean that the camera will not work, it will work ok, but in some cases it will save the image file with another name and not the name used in extra output

    Last edited: Jul 21, 2016
    Rockefeller Goldman likes this.
  6. Erel

    Erel Administrator Staff Member Licensed User

    The file name is not important and will not help with the issue of the camera app ignoring the "output" value.
    Rockefeller Goldman likes this.
  7. cxdzbl

    cxdzbl Active Member Licensed User

    Maven artifact not found:
    The same question, how can I do, to explain the specific operation of the procedure?

    B4A version: 6.00
    Parsing code. (0.00s)
    Compiling code. (0.08s)
    Compiling layouts code. (0.04s)
    Organizing libraries. Error
    Maven artifact not found:
  8. Douglas Farias

    Douglas Farias Expert Licensed User

    try put this jar on b4a libraries folder

    and add this to your code
    #AdditionalJar: android-support-v4
  9. cxdzbl

    cxdzbl Active Member Licensed User

  10. Douglas Farias

    Douglas Farias Expert Licensed User

  11. cxdzbl

    cxdzbl Active Member Licensed User

  12. freedom2000

    freedom2000 Well-Known Member Licensed User


    The example works fine (thank you)

    But is there a way to have the picture automatically taken, without having to click on the "button" ?

    Rockefeller Goldman likes this.
  13. Erel

    Erel Administrator Staff Member Licensed User

    No. Use CameraEx for this.
    freedom2000 likes this.
  14. freedom2000

    freedom2000 Well-Known Member Licensed User

    Ok thank you .
  15. TheArkhangel

    TheArkhangel Member Licensed User

    Just say that I have tried the example directly into my B4A version 5.80 and it works perfectly, activates the camera and records the image.

    Erel take this opportunity to congratulate the great work that is being done with this tool that we use both.

    Greetings to all.
    Erel likes this.
  16. TheArkhangel

    TheArkhangel Member Licensed User

    One question about it, I try to use the example to mixed with ftp send file......
    the path of photo and the name who variable used to include at the ftp command ?

    FTP.UploadFile(File.DirRootExternal, file, False, "/remotepath/", file)

    Sub TakePicture
    Dim i As Intent
    Dim uri As Uri
    File.Delete(imageFolder, tempImageFile)
    uri.Parse("file://" & File.Combine(imageFolder, tempImageFile))
    "output"uri'the image will be saved to this path
    ToastMessageShow("Camera is not available."True)
    End Try
    End Sub
  17. Erel

    Erel Administrator Staff Member Licensed User

  18. TheArkhangel

    TheArkhangel Member Licensed User

    Whats the best metod to delete de image take for camera device, the image available inside the ion_Event..... ?
    Last edited: Oct 9, 2016
  19. Erel

    Erel Administrator Staff Member Licensed User

    Sub ion_Event (MethodName As String, Args() As Object) As Object
    If Args(0) = -1 Then
    Dim in As Intent = Args(1)
    If File.Exists(imageFolder, tempImageFile) Then
             lastPicture = 
    LoadBitmapSample(imageFolder, tempImageFile, ImageView1.Width, ImageView1.Height)
             ImageView1.Bitmap = lastPicture
    File.Delete(imageFolder, tempImageFile) '<-----------------------
           Else If in.HasExtra("data"Then 'try to get thumbnail instead
             Dim jo As JavaObject = in
             lastPicture = jo.RunMethodJO(
    End If
    End Try
    End If
    If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
    Return Null
    End Sub
  20. Antonio Eduardo

    Antonio Eduardo New Member Licensed User

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice