I’m trying to create a Unity plugin and wrote the following Java code to copy data in a mobile device.
package com.u7.unityplugin;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class PluginU7 extends UnityPlayerActivity{
private File Dst;
public void setDst(String dst){
Dst = new File(dst);
}
public void loadImage() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 88763);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == 88763 && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
File Src = new File(uri.getPath());
try {
copyFile(Src, Dst);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
}
Since I need to let users choose which folder to copy, inheriting from UnityPlayerActivity and overriding the onActivityResult method is necessary. I’m currently calling my plugin method using the following Unity code.
AndroidJavaObject actObj = new AndroidJavaObject("com.u7.unityplugin.PluginU7");
actObj.Call("setDst", Application.persistentDataPath);
actObj.Call("loadImage");
And I got the following error at startActivityForResult().
Error Unity AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
I did some research and found a suggestion that I need to use context.startActivityForResult. Is this the right approach? Where can I get the Context?
for aar
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.u7.unityplugin">
<application
android:allowBackup="true"
android:supportsRtl="true">
<activity
android:name=".PluginU7"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
</activity>
</application>
</manifest>
for unity apk
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<activity android:name="com.u7.unityplugin.PluginU7"
android:theme="@style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
Also, this is my Manifest file, could you please check if there are any issues?"