Load Image in and outside data folder

@Chrisir, @noel, @Nik_Gun ===
there are 3 or 4 questions in this post:

  • first one was (@Nik_Gun) how to load images from sub folder inside the data folder without the error explained in ths post (this file contains a path separator…) ; answer from @Chrisir does not works in this case.With android it will fire the same error . From a technical point of view that is the result of the classical way for loading an image (or some file) with P5: it uses fileInput and fileInput is waiting for the name of the file, and not the complete path (with 1 or more pathSeparators).
  • @noel explains another thing= how to get a file from the external dir storage or save it.That is not the same problem i think; and the other presision (“scopedStorage”) though it could be important it does not matter here (of course what is said about permissions by @noel is exact)
    In order to answer to @Nik_Gun (and others in the same case) the best way for android is to use the android AssetManager Api and kook inside the data directory : in the code below i have 2 subfolders inside data , the first one is callled “autredos” the second is called autresousdoss; here you put your images. The code below will get a image from the “autresousdos”; that will be done through the “special” loadImages function which is able to add all images in an array: then you choose the image from the list. Try!

import android.content.Context;
import android.app.Activity;
import android.content.res.AssetManager;

PImage imga;// the image which is inside the subfolder called "autredos" and you want to get
Activity act;
String finalPath;
PImage[] img;// arrayList for all images

void setup(){
  size(600,400);
  background(255);
  act= this.getActivity();
  img = loadImages("autredos"+"/"+"autresousdos");// for creating the list in the subfolder
  imga = img[0];// here i suppose that i know the image i will display; in some cases you have to create a list view and can choose from it
  
  
}

void draw(){
  background(255,0,0);
  image(imga, 0, 0); //displaying the image imaga, in the subfolder
}


PImage[] loadImages(String folderName_){
  
  
  AssetManager assetManager = act.getAssets();
  try{
    String[] imgPath = assetManager.list(folderName_);
    println(imgPath.length);
    File[] files = new File[imgPath.length];
    
    img = new PImage[imgPath.length];
     for(int i=0; i<imgPath.length; i++){
       println (imgPath[i]);
    
     img[i] = loadImage(folderName_ + "/" + imgPath[i]);
     files[i] = new File(folderName_+"/" + imgPath[i]);//pour des fichiers quelconque, here you can find all folders
     println("file=====" + files[i]);
    }
    
    
  }
  catch(IOException ex){
       System.out.println (ex.toString());   
  }
  
  return img;
}
4 Likes