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

Time:03-21

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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ijusttrytoloadanimage">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ijusttrytoloadanimage"
        >
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

CodePudding user response:

I finally found the answer. If anyone else has the same problem, just do the following:

  1. In Project View right-click on app and select New -> Directory. Type the name "assets" in the new window and select src/main/assets from below.
  2. Copy your image file to the new directory app/src/main/assets.
  3. Load your image in your Sketch.java file with the command loadImage(your_image.png);

It works!

  • Related