Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to start activity - java.lang.NullPointerException when trying to load ffmpeg with ProcessBuilder #1830

Closed
publioelon opened this issue Jun 18, 2022 · 2 comments

Comments

@publioelon
Copy link

publioelon commented Jun 18, 2022

Greetings, I've been trying to use this library's ffmpeg resources but it seems to be crashing. I am using a default activity to attempt to stream a randomly generated image, for the sake of testing I tried to simply call ffmpeg using an echo command. If I do something like: ProcessBuilder pb = new ProcessBuilder("echo", "Hello World!"); it will work without any issues. However, if I attempt to pass ffmpeg by doing: ProcessBuilder pb = new ProcessBuilder(ffmpeg, "Hello World!"); it gives me NullPointerException error, or whatever command I attempt to use with ffmpeg after I load it, it'll give me the same error. For instance, I tried converting some video by using this command: ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-i", "/storage/emulated/0/DCIM/20220617_220627.mp4", "-c:v", "libx264", "-crf 19", "destinationfile.flv"); The same error happened. So, I suspect there is something wrong with my FFMPEG version, perhaps? I tried installing it following this link: #1117 (comment)

this is the snippet that I am using:

package com.example.myapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.bytedeco.javacpp.Loader;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    static final String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProcessBuilder pb = new ProcessBuilder(ffmpeg, "Hello World!");
        pb.redirectErrorStream(true);
        System.out.println("Run echo command");
        Process process = null;
        try {
            process = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int errCode = 0;
        try {
            errCode = process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Echo command executed, any errors? " + (errCode == 0 ? "No" : "Yes"));
        try {
            System.out.println("Echo Output:\n" + output(process.getInputStream()));
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            OutputStream writer = process.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e("TAG", "void OnCreate called successfully!");

    }

    private static String output(InputStream inputStream) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + System.getProperty("line.separator"));
            }
        } finally {
            br.close();
        }
        return sb.toString();
    }

}

My build.gradle:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
configurations {
    javacpp
}
task javacppExtract(type: Copy) {
    dependsOn configurations.javacpp

      from { configurations.javacpp.collect { zipTree(it) } }
    include "lib/**"
    into "$buildDir/javacpp/"
    android.sourceSets.main.jniLibs.srcDirs += ["$buildDir/javacpp/lib/"]

    tasks.getByName('preBuild').dependsOn javacppExtract
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation group: 'org.bytedeco', name: 'javacv', version: '1.5.6'
    javacpp group: 'org.bytedeco', name: 'openblas-platform', version: '0.3.17-1.5.6'
    javacpp group: 'org.bytedeco', name: 'opencv-platform', version: '4.5.3-1.5.6'
    javacpp group: 'org.bytedeco', name: 'ffmpeg-platform', version: '4.4-1.5.6'
}

It gives me the following error constantly:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 18328
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4035)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8663)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: java.lang.NullPointerException
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1012)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:8290)
        at android.app.Activity.performCreate(Activity.java:8270)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4009)
@saudet
Copy link
Member

saudet commented Jun 18, 2022

Android won't let you start new processes when using API 29 or newer, that's normal, see issue #1127 (comment).

@saudet
Copy link
Member

saudet commented Jun 18, 2022

Duplicate of #1127

@saudet saudet marked this as a duplicate of #1127 Jun 18, 2022
@saudet saudet closed this as completed Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants