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

**URL:** <https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860>\
**Category:** Processing for Android\
**Created:** [March 20, 2022, 4:40pm UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860 "2022-03-20T16:40:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pantsik](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@pantsik](https://discourse.processing.org/u/pantsik)\
**Post date:** [March 20, 2022, 4:40pm UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/1 "2022-03-20T16:40:40Z")

</div>

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>

```

/\>

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [March 20, 2022, 8:35pm UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/2 "2022-03-20T20:35:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pantsik](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@pantsik](https://discourse.processing.org/u/pantsik)\
**Post date:** [March 21, 2022, 6:19am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/3 "2022-03-21T06:19:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SantosCuccia](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@SantosCuccia](https://discourse.processing.org/u/SantosCuccia)\
**Post date:** [March 30, 2022, 5:50am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/4 "2022-03-30T05:50:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![comomomo](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@comomomo](https://discourse.processing.org/u/comomomo)\
**Post date:** [July 9, 2023, 2:10am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/5 "2023-07-09T02:10:39Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 9, 2023, 6:13am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/6 "2023-07-09T06:13:24Z")

</div>

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](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:**

 ![files](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/3/93d410a0906ef5762eb804a4dcdadd67ee73be07.png)

**Main Activity:**

```auto
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:**

```auto
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:**

 ![scrnshot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/a/7aa5e0d7caa33d0e4ea2fa3b20b3f144b3ca7be7.jpeg)

---

<div class="post-metadata">

**Author:** ![comomomo](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@comomomo](https://discourse.processing.org/u/comomomo)\
**Post date:** [July 9, 2023, 7:46pm UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/7 "2023-07-09T19:46:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 9, 2023, 10:28pm UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/8 "2023-07-09T22:28:35Z")

</div>

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.

```auto
PImage img;

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

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

```

---

<div class="post-metadata">

**Author:** ![comomomo](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@comomomo](https://discourse.processing.org/u/comomomo)\
**Post date:** [July 10, 2023, 12:22am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-android-studio-using-a-processing-sketch/35860/9 "2023-07-10T00:22:35Z")

</div>

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