Android Question Image cropping with camera intent

kostefar

Active Member
Licensed User
Dear All,

For capturing a camera image, the below works fine:

B4X:
Dim i As Intent
    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    Dim uri As Uri
    i.PutExtra("crop", "true") 
    i.PutExtra("aspectX", 0) 
    i.PutExtra("aspectY", 0) 
    i.PutExtra("outputX", 100)
    i.PutExtra("outputY", 100) 
    uri.Parse("file://" & File.Combine(imageFolder, tempImageFile))
    i.PutExtra("output", uri)

    Try
        StartActivityForResult(i)

But I don´t get to crop it, all the putextras for that purpose are completely ignored.
Anyway, I then tried to do this, using the location of where the image has been put and is confirmed to exist:

B4X:
Dim uri As Uri
    uri.Parse("file://" & File.Combine(imageFolder, tempImageFile ))
    in.Initialize("com.android.camera.action.CROP", uri)
   
    in.Flags = 1
    in.SetType("image/*")
    in.PutExtra("crop", "true")
    in.PutExtra("aspectX", 1)
    in.PutExtra("aspectY", 1)
    uri.Parse("file://" & File.Combine(imageFolder, "tempimg2.jpg"))
    in.PutExtra("output", uri)

But this throws me an error:

B4X:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=(StringUri) file:///storage/emulated/0/Android/data/b4a.example4/files/tempimage.jpg typ=image/* flg=0x20001 (has extras) }

If somebody knows how to get the cropping to work with the first part, why I get an exception with the 2nd one is not really important - I just need this to be solved either way.

Thanks in advance!
 

kostefar

Active Member
Licensed User
It is up to the default camera app to respect those parameters. Why don't you crop the image yourself? It will be simpler and safer.

Thanks Erel, good to know. Well, the user needs to interact with the cropping, that´s why.
 
Upvote 0

DonManfred

Expert
Licensed User
Maybe you can use this?
 
Upvote 0

kostefar

Active Member
Licensed User
Maybe you can use this?

That looks fantastic and could be a solution here, though it seems a bit overkill. But then at least there´s a good looking way out if I don´t get it done the intent way - thanks!

Btw, i.ACTION_PICK works fine here first opening the gallery and afterwards letting me crop the selection, so I was wondering if there´s not a way to simply bypass the gallery opening up and instead just open one file - the image captured with android.media.action.IMAGE_CAPTURE?
If I do this:

B4X:
i.Initialize(i.ACTION_PICK, ParseUri("file://" & File.Combine(imageFolder, tempImageFile)))
it will still show the file selection dialog.

Thanks in advance.

EDIT: Ok, tired of trying to get these things to work so I tried your library Manfred. I´ve added a question in the thread you´re linking to.
 
Last edited:
Upvote 0
Top