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!!!