Android Question Intent.ACTION_PICK, images not working on API Level 24+

Luiz Orlandini

Member
Licensed User
Hi there,

my image picker is not working on Android Nougat.

This piece of code works in all Android versions until API Level 24 and 25.

B4X:
Sub btnChoosePhoto_Click
    CropImageInGallery(File.DirRootExternal,"crop.jpg")
End Sub

Sub CropImageInGallery(Directory As String, PictureName As String)
    Dim i As Intent
    i.Initialize(i.ACTION_PICK, "")
    i.SetType("image/*")
        i.PutExtra("output", ParseUri("file://" & File.Combine(Directory, PictureName))) 'output folder you set OpenCam(File.DirRootExternal, "1.jpg")
    i.PutExtra("crop", "true") 'crop ON
    i.PutExtra("aspectX", 1) 'crop aspects
    i.PutExtra("aspectY", 1) 'crop aspects
    i.PutExtra("outputX", 200) 'crop size
    i.PutExtra("outputY", 200) 'crop size
    StartActivity(i)
End Sub

Any thoughts on how to solve that?

This is another approach, but I also need to crop... Struggling with that

B4X:
    Dim c As ContentChooser
    c.Initialize("CC")
    c.Show("image/*", "Select an image")

Cheers.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
On Api 23+ you MUST USE RUNTIMEPERMISSIONS.
 
Upvote 0

Luiz Orlandini

Member
Licensed User
This came from a client though in a release version.

Since I'm using a Macbook with a Virtual Machine I can't run it under a AVD at all.

I spent a bunch of time reading about it and found something that could explain this issue. This might be happening because of StrictMode Android N has do not support pass a file:Uri in an Intent extra anymore. We should use FileProvider instead.

I tried different approaches to crop the file, but I'm struggling still.

B4X:
Sub CropImageInGallery(Directory As String, PictureName As String)
    Try 
        Dim i As Intent
        i.Initialize("com.android.camera.action.CROP", "")
        i.SetType("image/*")
        i.Flags = 1
        i.PutExtra("output", ParseUri(Directory & PictureName))
        i.PutExtra("crop", "true") 'crop ON
        i.PutExtra("aspectX", 1) 'crop aspects
        i.PutExtra("aspectY", 1) 'crop aspects
        i.PutExtra("outputX", 200) 'crop size
        i.PutExtra("outputY", 200) 'crop size
        StartActivity(i)
    Catch
        Log(LastException)
    End Try
End Sub

Sub ParseUri(FileName As String) As Object
    Dim r As Reflector
    Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(FileName), Array As String("java.lang.String"))
End Sub

This codes it's executed, I could pick an image although the imaged loaded to crop is always black.

Almost there, but looks like theres some piece missing to make it work.

Any thoughts?
 
Last edited:
Upvote 0

Pravin Shah

Member
Licensed User
Longtime User
Hi, I am using this thread as my problem is same as posted in the first post. I am using intent.action_pick to get the image from gallery, crop it and then display it using imageview. I have used same code as in first post and it was working properly earlier. But now it is not working. It does show me Gallery but when I select an image, the code returns nothing. I am not sure if it is a issue related to api level. It do not throw any error as well.

I have tried searching for solution but did not get any proper answer. Would appreciate your help to fix this issue. Thank you
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi, I am using this thread as my problem is same as posted in the first post
does not matter

1. This is an old thread!
2. ALWAYS create a new thread for YOUR question. Post al relevant infos und the complete error. Best upload a small project which shoes the problem.

Edit to add: Check the Fileprovider example.
 
Last edited:
Upvote 0
Top