How to load an image in Android Studio using a Processing sketch

0

I cannot load an image using a Processing sketch.

I am using Android Studio on Windows 10 with an android emulator. I have placed an image file called “car.png” in the following directory: E:\Android\AndroidStudioProjects\car\app\src\main\res\drawable

I also placed it in the following directory using the Device File Explorer of Android Studio: /data/user/0/com.example.ijusttrytoloadanimage/files/

When I double-click it from the Project tree or from the Device File Explorer, it shows the car image.

But when I run my project in Android Emulator I get the following error message: W/System.err: java.io.FileNotFoundException: /data/user/0/com.example.ijusttrytoloadanimage/files/car.png (Permission denied)

This is my Sketch.java file:

<
package com.example.ijusttrytoloadanimage;

import processing.core.PApplet;
import processing.core.PImage;

public class Sketch extends PApplet {
public PImage car;

public void settings() {
    fullScreen();
}

public void setup() {
    imageMode(CENTER);
    car = loadImage("car.png");
}

public void draw() {
    background(0);
    translate((float) width/2, (float) height/2);
    if (car != null) {
        image(car, 0, 0);
    } else {
        print("nothing shows up!");
    }
}

/>

The message I get when running the Sketch.java is “nothing shows up!” meaning that the PImage variable car is still empty.

Why I get this permission denied message although I use the internal storage which by default has its access granted? What should I do to show my image file to the emulator’s screen?

This is my MainActivity.java file:

<
package com.example.ijusttrytoloadanimage;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import processing.android.PFragment;
import processing.android.CompatUtils;
import processing.core.PApplet;

public class MainActivity extends AppCompatActivity {
private PApplet sketch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CompatUtils.getUniqueViewId());
    setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    sketch = new Sketch();
    PFragment fragment = new PFragment(sketch);
    fragment.setView(frame, this);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (sketch != null) {
        sketch.onRequestPermissionsResult(
                requestCode, permissions, grantResults);
    }
}
@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (sketch != null) {
        sketch.onNewIntent(intent);
    }
}

}
/>

This is my AndroidManifest.xml file:

<

<?xml version="1.0" encoding="utf-8"?>






            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
/>

https://stackoverflow.com/questions/71527046/how-to-load-an-image-in-android-studio-using-a-processing-sketch

Yes. I figured it out myself and I posted it on stack overflow for others who might have the same problem.

Very useful advice. I think I’ll try it,thanks!!!

hi! I’m having similar issues right now on a sketch. I have the folders setup like @svan 's link with a “src/main/assets/” folder. When I try to load an image from there I get this message which doesn’t seem consistent with everyone elses errors.

java.lang.NullPointerException: Attempt to invoke interface method 'android.content.res.AssetManager processing.core.PSurface.getAssets()' on a null object reference
at processing.core.PApplet.createInputRaw(PApplet.java:5006)
at processing.core.PApplet.createInput(PApplet.java:4863)
at processing.core.PApplet.loadImage(PApplet.java:4003)
at com.developer.app.Playback.setupIcons(Playback.java:43)
at com.developer.app.Playback.<init>(Playback.java:30)
at com.developer.app.Sketch.setup(Sketch.java:21)
at processing.core.PApplet.handleDraw(PApplet.java:1878)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:478)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:518)

Can anyone tell just based on this what’s going on in my Android Studio project? My MainActivity class is based off of the example on the site.

  1. The error message states that it’s having trouble finding your ‘assets’.

  2. I just tried adding an image to my Android tablet using the instructions found here: https://android.processing.org/tutorials/android_studio/index.html in combination with the stackOverflow link shown above and it worked without error on my system (macOS). I’m posting relevant code in hopes that you can find the problem.

Project files:

Main Activity:

package com.example.processingdemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import processing.android.PFragment;
import processing.android.CompatUtils;
import processing.core.PApplet;

public class MainActivity extends AppCompatActivity {
    private PApplet sketch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frame = new FrameLayout(this);
        frame.setId(CompatUtils.getUniqueViewId());
        setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        sketch = new Sketch();
        PFragment fragment = new PFragment(sketch);
        fragment.setView(frame, this);
    }
}

Sketch class:

package com.example.processingdemo;

import processing.core.PApplet;
import processing.core.PImage;
public class Sketch extends PApplet {
    PImage img;
    public void settings() {
        size(1600, 1600);
    }
    public void setup() {
        img = loadImage("android.png");
    }
    public void draw() {
      image(img,100,200);
    }
}

Android tablet output:

1 Like

Ah! I figured it out. Thanks for posting this. The issue was that I was extending PApplet on classes other than Sketch. I’m not sure what the best approach is for having multiple classes in this case. I started just using a singleton pattern public static Sketch instance; but I’m not super happy with it.

You probably already know this, but you can get the same output with the following source code using the Processing editor with Android mode. The image file is in a ‘data’ folder in the sketch folder.

PImage img;

void setup() {
  fullScreen();
  background(204);
  img = loadImage("android.png");
}

void draw() {
  image(img,200,200);
}

Yep! I ended up using some packages that were easier to manage in Android Studio which is why I switched over.