Why return null length in Android mode?

edited August 2017 in Android Mode

This function in java mode works well.. but in android mode returns null length of folder.. any solution??

PImage[] loadImages(String nameFolder) {
  java.io.File folder = new java.io.File(dataPath("")+"/"+nameFolder);
  String[] filenames = folder.list();
  //println(filenames.length);
  //println(dataPath("")+"/"+nameFolder);
  PImage[] img = new PImage[filenames.length];
  for (int i = 0; i < filenames.length; i++) {
    //println("Loading: "+filenames[i]);
    img[i] = loadImage(dataPath("") + "/" + nameFolder + "/" + filenames[i]);
  }
  return img;
}
Tagged:

Answers

  • First question: did you enable read permissions?

    Then, consider checking this post:

    https://forum.processing.org/two/discussion/21424/using-android-studio-how-to-i-load-media-files#latest

    For instance: https://forum.processing.org/two/discussion/comment/61200/#Comment_61200
    https://forum.processing.org/two/discussion/21167/accessing-sd-cards-for-storage#latest

    That last one might not help but it is for reference.

    I have to say this was tested with Android Mode 3.0.2ish or earlier. Not sure if it works with AM 4.0 beta 9 (latest version I think).

    Kf

  • edited August 2017 Answer ✓

    @erkosone===

    your code is ok in java mode but it cannot work in android mode because when compiling P5 creates a folder for "assets" replacing the data folder and its contents (in your case some subfolder with images).That's why it returns null: data folder does not exist anymore... So you have to use AssetManager from android for retrieving all your images, putting them in a PImage[] and loading them.

  • Any example of this AssetManager for load a folter with images?

  • @erkosone===

    code snippet below; the subfolder "dossier" is in the data folder (at the begining!) - In my case i have put only 1 image inside it; but of course you can change that && get all images with a for loop.

        import android.content.Context;// useless here but...
        import android.app.Activity;
        import android.os.Environment; // useless here but...
        import android.content.res.AssetManager;
        import android.graphics.Bitmap;//useless here but if you want to use android for //transforming the image when decoding from file inputstream....
    
        PImage imga;
        Activity act;
    
    
    
        void setup(){
    
          size(600,400);
          background(255);
          act= this.getActivity();//for fragments
          AssetManager assetManager = act.getAssets();
          try{
          String[] imgPath = assetManager.list("dossier");
          println (imgPath);
          imga = loadImage(imgPath[0]);//only 1 in the subfolder....
    
          }catch(IOException ex){
               System.out.println (ex.toString());   
           }
        image(imga, 0,0);
        };
    
  • Hi, y try to work with this example but not works.. the same error apears. Null pointer access.

    Can you check it? https://www.dropbox.com/s/sltcqu65grhzz9q/testAssetManager.rar?dl=0

  • DOWNLOAD EXAMPLE: https://www.dropbox.com/s/sltcqu65grhzz9q/testAssetManager.rar?dl=0

    import android.content.Context;// useless here but...
    import android.app.Activity;
    import android.os.Environment; // useless here but...
    import android.content.res.AssetManager;
    import android.graphics.Bitmap;//useless here but if you want to use android for //transforming the image when decoding from file inputstream....
    //........................................................
    PImage[] img;
    //........................................................
    void setup(){
      size(displayWidth, displayHeight);
      orientation(PORTRAIT);
      img = loadImages("cat/");
      image(img[0], 0, 0);
    }
    //........................................................
    PImage[] loadImages(String folderName_){
      Activity act;
      PImage[] img_ = null;
      act = this.getActivity();
      AssetManager assetManager = act.getAssets();
      try{
        String[] imgPath = assetManager.list(folderName_);
        println(imgPath.length);
        for(int i=0; i>imgPath.length; i++){
          img_[i] = loadImage(imgPath[i]);
        }
      }
      catch(IOException ex){
           System.out.println (ex.toString());   
      }
      return img_;
    }
    //........................................................
    
  • edited August 2017

    @erkosone=== of course your code cannot run because you transformed mine in a weird way:

    • first error is line 24 : i > must be i<....

    • second error with your PImage[]: you have to initialize it with some length (imagPath) but you initialize it only to "null"...that 's why in my code i used an arrayList().

    • third error is with your slash: you have only to give the subfolder name; doing with a slash is useless and can fire an illegal argument exception

    • below my code rewritten to integrate your method ... try it!

      import android.content.Context;
      import android.app.Activity;
      import android.content.res.AssetManager;
      
      
      PImage imga;
      Activity act;
      String finalPath;
      ArrayList<PImage> images= new ArrayList<PImage>();
      PImage[] img;
      
      void setup(){
        size(600,400);
        background(255);
        act= this.getActivity();
        img = loadImages("dossier");
        imga = img[0];
        image(imga, 0, 0);
      
      }
      
      void draw(){
      
      }
      
      
      PImage[] loadImages(String folderName_){
      
      
        AssetManager assetManager = act.getAssets();
        try{
          String[] imgPath = assetManager.list(folderName_);
          println(imgPath.length);
          img = new PImage[imgPath.length];
           for(int i=0; i<imgPath.length; i++){
           img[i] = loadImage(imgPath[i]);
          }
      
      
        }
        catch(IOException ex){
             System.out.println (ex.toString());   
        }
        println(img.length);
        return img;
      }
      
  • edited August 2017 Answer ✓

    so???

  • Sorry, i try it today and reply to you. ;)

  • edited August 2017

    YOU ARE THE BOSS!!!!

    Works perfectly!!!!!!!

    This is the final code:

    import android.content.Context;
    import android.app.Activity;
    import android.content.res.AssetManager;
    
    
    Activity act;
    String finalPath;
    ArrayList<PImage> images= new ArrayList<PImage>();
    PImage[] img;
    
    void setup(){
      size(displayWidth, displayHeight);
      //background(255);
      img = loadImages("cat");
      image(img[0], 0, 0);
    
    }
    
    void draw(){
    
    }
    
    
    PImage[] loadImages(String folderName_){
      act= this.getActivity();
      AssetManager assetManager = act.getAssets();
      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;
    }
    

    Thank you very much, this is an excellent resource loader function, it saves me a lot of workavoiding reading files one by one.

    This function should be in the language directly available, it is very good.

Sign In or Register to comment.