1

I want to access a GoPro Hero 4 silver with the official GoPro WSDK but get an runtime exception when I try to access it with getMediaGateway(). I'm new in android development and in the GoPro developer documention I read that you have to do this in a background thread, but I can't get it to work. I have to do it with .postDelayed(). It's an other story, not important at the moment. I did it excatly the same way as described in the official GoPro SDK documentation, but I get the exception you see after my code snippet. In short: Camera records a video (with lopping mode), after 3 seconds the sd-card should be accessed. I found out the problem is this cameraresult-call, but can't fix it, even when I test it with .isSuccess().

    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // [...]

    // App shutter-button onClickListener
    mBtnShutter.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(final View v) {
            doCameraTasks(3,v);
        }
    });
}

// Press shutter after n seconds and extract both video-parts
private void doCameraTasks(int seconds, final View v) {
    // On
    getActivity().startService(C2Service.newToggleShutterCommand(v.getContext(),
            mGoProCamera.getGuid()));

    v.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Off after n seconds
            getActivity().startService(C2Service.newToggleShutterCommand(v.getContext(),
                    mGoProCamera.getGuid()));
       }
    },seconds*1000);

    v.postDelayed(new Runnable() {
        @Override
        public void run() {
            CameraCommandResult<MediaResponse<GpCameraMedia>> cameraresult = mGoProCamera.getMediaGateway().getMediaList();

            if(cameraresult.isSuccess()) {
                MediaResponse<GpCameraMedia> mediaResponse = cameraresult.getData();
                List<GpCameraMedia> myMediaList = mediaResponse.getMedia();
                String cardID = mediaResponse.getSdCardGuid();
                Log.d("sdcard_id",cardID);
            }
        }
    },(seconds+2)*1000);
}

Exception:

com.gopro.wsdksample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.gopro.wsdksample, PID: 26823
                                                                  android.os.NetworkOnMainThreadException
                                                                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
                                                                      at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
                                                                      at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
                                                                      at java.net.PlainSocketImpl.read(PlainSocketImpl.java:549)
                                                                      at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:42)
                                                                      at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:253)
                                                                      at com.android.okio.Okio$2.read(Okio.java:113)
                                                                      at com.android.okio.RealBufferedSource.indexOf(RealBufferedSource.java:147)
                                                                      at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:94)
                                                                      at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
                                                                      at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
                                                                      at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
                                                                      at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
                                                                      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
                                                                      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491)
                                                                      at com.gopro.wsdk.domain.camera.SingleConnectionHttpClient.send(SingleConnectionHttpClient.java:60)
                                                                      at com.gopro.wsdk.domain.camera.network.wifi.GpControlHttpCommandSender.send(GpControlHttpCommandSender.java:81)
                                                                      at com.gopro.wsdk.domain.camera.operation.media.GetCameraMediaCommand.execute(GetCameraMediaCommand.java:64)
                                                                      at com.gopro.wsdk.domain.camera.network.wifi.GpControlHttpCommandSender.process(GpControlHttpCommandSender.java:76)
                                                                      at com.gopro.wsdk.domain.camera.sender.CommandSenderManager.process(CommandSenderManager.java:148)
                                                                      at com.gopro.wsdk.domain.camera.GoProCamera.process(GoProCamera.java:426)
                                                                      at com.gopro.wsdk.domain.camera.operation.media.GpMediaGateway.getMediaList(GpMediaGateway.java:85)
                                                                      at com.gopro.wsdksample.features.ControlFragment$3.run(ControlFragment.java:112)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5306)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  • postDelayed does not put things on a background thread, it runs it on the handler's thread with a delay. You need an actual background thread. – Gabe Sechan Aug 04 '17 at 14:50
  • it's about strictmode add this ` StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` after super.onCreate() – Oussema Aroua Aug 04 '17 at 14:52
  • wrong duplication flag, the accepted answer have nothing with this question – Oussema Aroua Aug 04 '17 at 14:53
  • Thank you @OussemaAroua. It works now, but I don't understand what exactly this does. developer.android.com says "StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them." And with permitall() I disable detection of everything. But why does this work and I got rid of a "NetworkOnMainThreadException"? – user8417839 Aug 07 '17 at 13:17
  • Ah, I think I got it now. I refactored my code (I created an AsyncTask into a TimerTask) and it works without this StrictMode-workaround. – user8417839 Aug 07 '17 at 14:23
  • Can Anyone give me any hint where can i get goPro official wsdk. – Praveen Sharma Jan 16 '19 at 08:03

0 Answers0