How do a get image files on to my android?

edited February 15 in Android Mode

Howdy, I'm new to processing and new to Androi so I suspect i have a conceptual block here. I've written a very simple routine that is trying to load a background image. Code Follows. It runs fine in Java but when I try to run it on the Android Emulator I'm getting a

java.io.FileNotFoundException: /data/user/0/processing.test.wrapmate/files/bg.jpg (No such file or directory)

I'm assuming this is because my image file is sitting on my computer and not on my Device (either a real one or the Emulator).

So a) during development, what's the way to get any image files I need to access to the right place on the Android so that the program can access them?

b) If I eventually want to "deploy" an app to my friend's devices (like through the App Store?) how do I ensure that any image files come along with the application?

Thanks!

Dave

edit... trying to learn to format code in the forum. Sorry!

void setup()
{
  size(displayWidth, displayHeight, P3D);
}

void draw()
{
  //noStroke();
  PImage back; 
  back = loadImage("bg.jpg");
  back.resize (displayWidth, displayHeight); 
  background(back);

  float box_width = 60;

  if (mousePressed) {
    box_width = mouseX;
  }

  lights();
  translate(width/2, height/2, 0);
  rotateX(-PI/6);
  rotateY(PI/2 + 210/float(height) * PI);
  box(box_width, displayHeight/6, displayWidth/3);
}
Tagged:

Answers

  • kfrajer, thanks for your note on formatting my code and I apologize. I'm clearly much newer to this and have very much to learn. I've tried reading through the posts you linked to and will continue to do so. But i suspect my conceptual block is deeper. In my example, I'd like to simply load a .jeg as a background image on the android. But the .jpeg is currently an image on my computer. So I understand it needs to get onto the android. And I understand there are different locations I can place it. And that I need to write some code that moves the file from my computer onto the adroid. But I'm confused if that code executes separately from my intended sketch and moves the files independently, after which the file is available for use? This seems unlikely to be an efficient process so I clearly need to learn more. I know it's not your responsibility to educate me but aside from actual code examples, are you able to conceptually explain how an image file I accessed on my computer ends up on my android accessable for that program? And if I need to move this file independently from my sketch in a two step process how does this occur when deploying the app to a new phone? Sorry I'm so new at this and feel free to ignore. I'll keep trying to learn more.

  • And that I need to write some code that moves the file from my computer onto the adroid.

    Nop. You don't have to do that. Everything gets packed into an apk file and loaded into your phone. Questions for you: Have you run a simple sketch into android before? (nothing wrong if it is your first time) Have you setup your SDK and Android mode properly? Have you enabled the Developer Option in your phone?


    Technically, if your sketch is simple, you can run it first in java mode. Place all your resources in the data folder (I hope you know this. If not, let me know or check the PImage in the reference). After you test your app in java mode and it runs, you can switch to Android mode ( I am assuming you have setup your Android mode and that you have run the first example as explained by codeanticode here ). In Android mode, you need to go to Android>>permissions menu and click on write_external_storage permission. SECOND step: You need to provide full path to your resources. This implies changes as I will explain next:

    If you have a call to load an image like this: PImage img=loadImage("Berlin.jpg"); then it must be changed to PImage img=loadImage(dataPath("")+"Berlin.jpg");

    If you decide to store your files in your external SD storage, then you need to follow the instructions as described in one of the above links. I need to warn you those posts were done using an older version of AM (Android Mode). They might work in the latest version of AM or there are potentially minor adjustments to get them running properly.

    Now, the concept is this. After you set your environment into AM, check the permissions, provide proper path for your files, then if Processing recognizes your device, hitting in the Run button should load the sketch into your phone. Under the hood, Processing uses Graddle to package your Android app. This generates an unsigned apk file which is loaded and executed in your device. NOTE: Generating and loading the apk can take between 10 to 25 seconds... so be patient.

    I suggest, if you haven't done so, to become familiar with http://android.processing.org/tutorials/getting_started/index.html as well as the other sections in this site as it will provide minimal info to get you started. Don't be afraid to browse the forum. I strongly recommend you use google instead of the forum search engine as the latter is simply not robust. Finally, ask questions in the forum.

    Kf

  • Wow.. thank so very much for this detailed response. This is incredibly kind. I'm trying to teach myself Processing in the evenings so I won't be able to look at this in detail until later but I'm sure it will help tremendously. I'm a - shall we say - older individual with some limited ancient programming experience back in the days of Fortran. I know a bit of C and did some embedded work many years ago but things have changed so much! Unfortunately I've never had exposure and consequently really struggle with the principles of object oriented programming. This has been a real frustration and I'd love to learn Processing if only to prevent some mental atrophy. It's been a slog so far but I have installed Android Studio, and a version of the Android SDK. I was able to enable developer mode on my Pixel and transfer a very simple "hello world" sketch to it which was a major victory. It took me longer to get the emulator working - for some reason I needed to fiddle with the bios on my computer but even that is working now. I'll try very hard to not abuse your kindness. I know there are tons of resources on here and I just need to get better at learning how to avail myself of them. Thanks again!

  • edited February 20

    I am still struggling with a very simple android sketch where I am trying to access a .jpg as a background image.

    I have loaded the image as a .jpg in a /data folder that I created within the sketch folder. I'm getting the following error:

    Any hints on what I'm doing wrong?

    FATAL EXCEPTION: GLThread 375 Process: processing.test.wrapmate, PID: 11495 java.lang.NullPointerException: src == null at java.lang.System.arraycopy(Native Method) at processing.opengl.PGraphicsOpenGL.setImpl(Unknown Source:28) at processing.core.PImage.set(Unknown Source:58) at processing.opengl.PGraphicsOpenGL.backgroundImpl(Unknown Source:4) at processing.core.PGraphics.background(Unknown Source:43) at processing.core.PApplet.background(Unknown Source:2) at processing.test.wrapmate.wrapmate.draw(wrapmate.java:41) at processing.core.PApplet.handleDraw(Unknown Source:150) at processing.opengl.PSurfaceGLES$RendererGLES.onDrawFrame(Unknown Source:13) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1571) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)

    import android.os.Environment;
    import android.os.Build ;
    import android.app.Activity;
    import android.content.Context;
    
    
    void setup()
    {
      size(displayWidth, displayHeight, P3D);
    }
    
    void draw()
    {
    
      //  PImage back; 
      //  back = loadImage("bg.jpg");
      //  back.resize (displayWidth, displayHeight); 
      //  background(back);
    
      float box_width = 60;
    
      if (mousePressed) {
        box_width = mouseX;
      }
    
      lights();
      translate(width/2, height/2, 0);
      rotateX(-PI/6);
      rotateY(PI/2 + 210/float(height) * PI);
      box(box_width, displayHeight/6, displayWidth/3);
    }
    
  • edited February 20

    Please tell me you are not getting this error when executing the commented lines in draw. I mean, why do you have those lines there? Draw is executing at a rate of 60 cycles per second meaning those lines (yes, loading the file) is executing at that same speed (or trying to).

    Now, for the time being, the following line should work back = loadImage("bg.jpg"); only if your renderer is the default, i.e. size(displayWidth, displayHeight);. However, the above line doesn't work under P3D as you get a NPE which I document next:

    //FATAL EXCEPTION: GLThread 181069 //Process: processing.test.sketch_180219b, PID: 7447 //java.lang.NullPointerException: src == null // at java.lang.System.arraycopy(Native Method) // at processing.opengl.PGraphicsOpenGL.setImpl(Unknown Source) // at processing.core.PImage.set(Unknown Source) // at processing.opengl.PGraphicsOpenGL.backgroundImpl(Unknown Source) // at processing.core.PGraphics.background(Unknown Source) // at processing.core.PApplet.background(Unknown Source) // at processing.test.sketch_180219b.sketch_180219b.draw(sketch_180219b.java:123) // at processing.core.PApplet.handleDraw(Unknown Source) // at processing.opengl.PSurfaceGLES$RendererGLES.onDrawFrame(Unknown Source) // at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1562) // at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1262)

    From the trace call, I can see the problem is the background() function call in OPENGL mode. (@akenaton @codeanticode) I am able to reproduce the problem with the following MCVE (plz ensure image is in the data folder):

    import android.os.Environment;
    import android.os.Build ;
    import android.app.Activity;
    import android.content.Context;
    
    PImage back; 
    
    void setup()
    {
      size(displayWidth, displayHeight, P3D);
      back = loadImage("bg.jpg");
    }
    
    void draw()
    { 
       background(back);                      <----NPE!
       //set(0,0,back);                           <----NPE!
       //image(back,0,0,width,height);  <--It works 
    }
    

    Notice that when using P3D:

    • either background() or set() throws an NPE and App crashes
    • It works normally if one uses image() instead,

    Using the default renderer, you can use either background/set/image and you won't have any problem. I tested this using AM 4.0.1 and P3.3.6 on Nougat. I will be opening a tix in github for the time being. ****EDIT: Issue here

    @davef_dci For now, you have two options. Either don't use the P3D renderer or if you want to use it, refrain on using background() or set() and instead use:

    void setup()
    {
      size(displayWidth, displayHeight, P3D);
      back = loadImage("bg.jpg");
       back.resize(width,height);
    }
    void draw()
    {       
       image(back,0,0);  
    }
    

    Kf

  • kfrajer,

    thank you very much. It is working basically so I am on to other matters. Your help is truly appreciated and I am very grateful. I'm trying to learn what i can from the forums and google searches but I obviously need to gain more experience before I stop making very basic mistakes.

    Thanks!

  • Kfajer, Could you let me know if you find a resolution to this bug? I tried loading the background image using the Image() command instead of background(). While this works somewhat I am getting some strange and unpredictable shape clipping with a 3D shape I am attempting to draw on top of the background image Using background() resolves this issue in JAVA but continues to crash on the Android. I'll keep looking for resolutions.

    I don't really understand the pros and cons of the diffferent renderers but I'm needing to draw 3D shapes so I was using P3D. If there is a different approach please let me know. Thanks once again.

    Dave

  • I can suggest using AM 4.0 instead of 4.0.1. I don't think I saw this problem before but I might be wrong as I do not work much with P3D there. The only way to find out is to load the previous Android mode version and see if the problem is there. Fixing this bug will require effort from the github community and based on the priority tag. In other words, nobody knows how long it could take to solve it. For the time being, post here if you have results running the prev AM.

    Kf

  • I removed AM 4.0.1 and installed 4.0 but the problem persists. I'll keep working on it to see if there is a problem on my end. Thanks for your help.

    Dave

Sign In or Register to comment.