Search code examples
javaandroidair

Issue displaying ImageView from Air Native Extension (android)


First I want to says that I'm beginner with java and android, I spent a lot of time trying to make this work and searching for an answer but couldn't help myself.

I'm working on an Air Native Extension. If for now I succeed on creating this ANE and get most of the calls working, when it comes to display an ImageView from this ANE, I'm stuck.

It seems like I need to know some stuff to display an ImageView correctly, so at launch I retrieve some infos like the activity, context and rootView.

public class MyContext extends FREContext 
{

    @Override
    public void dispose() 
    {

    }


    @Override
    public Map<String, FREFunction> getFunctions()
    {
        Configs.activity    = this.getActivity();
        Configs.context     = Configs.activity.getApplicationContext();
        Configs.decorView   = Configs.activity.getWindow().getDecorView();

        Map<String, FREFunction> map = new HashMap<String, FREFunction>();

        map.put("init"                  , new InitFunction());
        map.put("oneFunction"           , new myFunction());

        return map;
    }

}

Then I call this class to load an image from the web, once it's loaded I want to display it on the screen

public class GraphicLoader extends AsyncTask<URL, String, String>
{
    public ImageView    view;

    private URL         url;
    private int         width;
    private int         height;


    // Constructor
    public GraphicLoader (String url, int width, int height)
    {
        this.width  = width;
        this.height = height;

        try 
        {
            this.url    = new URL (url);
        } 
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
    }


    @Override
    protected String doInBackground(URL... params) 
    {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;

        try 
        {
            this.view   = new ImageView (Configs.context);
            view.setImageBitmap(BitmapFactory.decodeStream(url.openConnection().getInputStream(), null, options));

            RelativeLayout relativeLayout = new RelativeLayout(Configs.context);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            view.setLayoutParams(lp);

            relativeLayout.addView(view);
            Configs.activity.setContentView(relativeLayout, rlp);

        } 
        catch (IOException e) 
        {
            Configs.debug = Configs.debug + " GraphicLoaded error 2 " + e.getMessage();
            e.printStackTrace();
        }

        return null;
    }
}

My application then crash when going into the doInBackground function, at line "Configs.activity.setContentView(relativeLayout, rlp);" . I probably don't use this in the right way, i tried multiple way to do it, depending on what I found on the web, but nothing succeed and always turned into a crash.

How should it be done ?

Thanks for your help?


Solution

  • So I found the solution, just move this part of the code out of the AsyncTask :

    RelativeLayout relativeLayout = new RelativeLayout(Configs.context);
    
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT);
    
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                view.setLayoutParams(lp);
    
                relativeLayout.addView(view);
                Configs.activity.setContentView(relativeLayout, rlp);
    

    The app doesn't crash anymore and will display the ImageView correctly.