Android mode: select input not working.. How can I dynamically load images?

edited August 2017 in Android Mode

Hi there!,

I was trying to use the selectInputfor loading an image in processing android mode and it didn't work. I also tried to use the SelectFile library, which is unfortunately not working with the latest processing.

any other libraries or methods to dynamically load image?

Any help would be much appreciated!

Thanks in advance :)

Answers

  • @yousufalin===

    i am not sure to understand what you want: is it a file chooser dialog in order that the user can choose a file???

    Then: is this file supposed to be anywhere on the device (gallery ...sd card) OR is this file supposed to be in your app???

  • @akenaton, Thank you for your reply.

    you got the question right! . I would like to create a button which will allow me to load the image from my sdcard/gallery.

    :)

  • @yousufalin===

    in this case you can use an intent with action getContent.

  • @akenaton thank you for the clue.

    I am completely new to android stuff.. Was reading through some of the posts in StackOverFlow.. I was able to write only the following part

        import android.content.Intent;
    
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    
        PImage img;
    
        void setup(){
    
        fullScreen();
    
        }
    
        void draw(){
    
        background(0);
    
        intent.setType("image/*");
    
        startActivityForResult(intent,100);
    
        }
    

    with this code, I am able to access my device to search for images. But I don't know how to select an image from the gallery and load it as the image in image(img,0,0);

  • edited August 2017

    @yousufalin===

    you are on the good way! - Now=

    • add createChooser to your startActivityForResult()

    • add a method onActivityResult()

    • in this method get the path (URI) to the selected image with a cursor

    • load the image (from the URI) and do something with it

    • i give you only the guideline but i think that is better than complete code for learning

    • problems you probably have:

    1. The code is not exactly) the same if you are targetting API<18 or >18

    2. Android knows about Bitmaps, not about PImage and you have to look at Bitmaps in android docs, though i think (without having never done that) that there is a way to "translate" bitmaps into PImage

    3. In order to put your bitmap on screen you have to create an imageView.

    Try and tell me...

  • import android.content.Context;
    import android.app.Activity;
    import android.content.res.AssetManager;
    //------------------------------------------------------------
    PImage[] loadImages(String folderName_){
      Activity act= this.getActivity();
      AssetManager assetManager = act.getAssets();
      PImage img_[] = null;
      try{
        String[] imgPath = assetManager.list(folderName_);
        img_ = new PImage[imgPath.length];
         for(int i=0; i<imgPath.length; i++){
         img_[i] = loadImage(folderName_ + "/" + imgPath[i]);
        }
      }
      catch(IOException ex){
           System.out.println (ex.toString());   
      }
      return img_;
    }
    
  • usage:

    PImage img[] = loadImages("photos");

    This example load entire "photos" folder from assets of APK

  • edited August 2017

    @erkosone===

    of course && i do know as this is the code i have given on this forum...But that is NOT what @yusufalin was asking: select input and let the user choose an object...

Sign In or Register to comment.