Android Tutorial Intent Based Camera

Status
Not open for further replies.
Newer example: [B4X] [B4XPages] Intent based camera

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

It is based on this tutorial: https://developer.android.com/training/camera/photobasics.html

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.
 

Attachments

  • CameraIntent.zip
    9.5 KB · Views: 2,953
Last edited:

Phayao

Active Member
Licensed User
Longtime User
Hello,
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,
Chris
 

DonManfred

Expert
Licensed User
Longtime User

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
this code
B4X:
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 file: path 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.
B4X:
Private const tempImageFile As String = "tempimage.jpg"

will be ignored like posted here
https://www.b4x.com/android/forum/t...ack-from-camera-app-intent.11378/#post-132956

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
http://stackoverflow.com/questions/...them-with-a-custom-name-to-a-custom-destinati

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

B4X:
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

thx
 
Last edited:

cxdzbl

Active Member
Licensed User
Maven artifact not found: com.android.support/support-v4
This example uses an intent to take a picture with the default camera app.

It is based on this tutorial: https://developer.android.com/training/camera/photobasics.html

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.

* - It uses RuntimePermissions.GetSafeDirDefaultExternal which means that you need to add the external storage permission to older versions of Android with this manifest code:
B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)

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: com.android.support/support-v4
 

Douglas Farias

Expert
Licensed User
Longtime User
Maven artifact not found: com.android.support/support-v4


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: com.android.support/support-v4


try put this jar on b4a libraries folder
http://iddesenvolvimento.com.br/downloads/android-support-v4.jar

and add this to your code
B4X:
#AdditionalJar: android-support-v4
 

freedom2000

Well-Known Member
Licensed User
Longtime User
HI,

The example works fine (thank you)

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

Thanks
 

TheArkhangel

Member
Licensed User
Longtime 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.
 

TheArkhangel

Member
Licensed User
Longtime 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)

B4X:
Sub TakePicture
    Dim i As Intent
    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    Dim uri As Uri
    File.Delete(imageFolder, tempImageFile)
    uri.Parse("file://" & File.Combine(imageFolder, tempImageFile))
    i.PutExtra("output", uri) 'the image will be saved to this path
    Try
        StartActivityForResult(i)
    Catch
        ToastMessageShow("Camera is not available.", True)
        Log(LastException)
    End Try
End Sub

Thanks
 

TheArkhangel

Member
Licensed User
Longtime User
Whats the best metod to delete de image take for camera device, the image available inside the ion_Event..... ?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then
     Try
       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("getExtras", Null).RunMethod("get", Array("data"))
       End If
     Catch
       Log(LastException)
     End Try
   End If
   If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
   Return Null
End Sub
 
Status
Not open for further replies.
Top