Android loadImage outside sketch folder.

edited May 2018 in Android Mode

Hi. I am trying to load an image with the code below, but this will give the error:

The method loadImage(String) in the type PApplet is not applicable for the arguments (File) >

How to do this? Thanks.

import android.os.Environment;

   PImage img;
   File f;

   String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/imgFolder";
   String fileName = "noel_28_5_2018_9_9.png";
   f = new File(baseDir + "/" + fileName); 
   img = loadImage(f);

Answers

  • f.getAbsolutePath(); //Untested but it should work. Don't forget to enable the relevant permission.

    Consider moving to the new forum.

    @akenaton I hope to see ya in the "nouveau" forum as well.

    Kf

  • @kfrajer Thanks for the hint. My next question wll be on the new forum. As for this topic, I do not understand what you mean. If I use a println(f); It gives the exact location: /storage/emulated/0/imgFolder/noel_28_5_2018_9_9.png. The sketch does not give any errors when out-commented the loadImage(f);.

  • When you use print, it is very likely the print function is invoking the toString() method associated to the Objec class. In turned, this function overriden by the File class. I am not totally sure loadImage() can handle objects of type File. Generating the proper string path should work.

    Kf

  • edited May 2018

    @noel===

    with P5 loadimage() waits for a String and with your code you give it a File object: that is the problem...Where is your imgFolder???? in your app??? on the sdCard???

    @kfrajer === i am on the new forum where i put a post for a weird problem i got with standard processing (no android)...Still unsolved; must be a processing bug : if you get some idea, it s in the "uncategorized" category!

    EDIT: no it migrates here https://discourse.processing.org/t/strange-results-when-exporting/288

  • edited May 2018

    @akenaton. Yes of course you are wright. It was just a hopeless try. Because using a String like

    "/storage/emulated/0/imgFolder/noel_28_5_2018_9_9.png"

    gives the error:

    java.lang.IllegalArgumentException: File /storage/emulated/0/imgFoldernoel_28_5_2018_9_9.png contains a path separator

    On the other hand saving a file to the internal storage, not specifically the sketch folder, works pretty well. I have tried a lot of other things but they are to foolish to be posted. I hope you have some idea, because I already have a headache.

  • Answer ✓

    @noel===

    i ll post some snippet tomorrow here.

  • Thanks!

  • Thanks!

  • @kfrajer @akenaton

    Afterall so simple. Thanks!

    import android.os.Environment;
    
    PImage img;
    
    void setup(){
    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/imgFolder";
    String fileName = "noel_28_5_2018_9_9.png";
    String fileToOpen = baseDir + "/" + fileName;
    img = loadImage(fileToOpen);
    set(0,0,img);
    }
    
  • Great, thxs for sharing your solution. To clarify on your approach, do you create the imgFolder ahead of time? Sorry, I would test it myself but I don't have resources setup atm.

    Kf

  • edited May 2018

    @kfrajer Yes with this code I verify and create a new directory.if necessary

    import android.os.Environment;
    
      try {
        String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/imgFolder");
        File Folder = new File(directory);
        if (!Folder.exists()) {
          boolean success = true;
           success = Folder.mkdirs();
        }
      } catch (Exception e) {
         println("Error while creating directory:" + e);
      }
    
  • @noel Thxs for sharing your full solution.

    @akenaton: For that bug, it seems one needs to dig into the source code. I give it a try tomorrow or Thursday.

    Kf

Sign In or Register to comment.