Search code examples
javaandroidgoogle-project-tango

Project Tango: Export ADF to sdcard


I'm trying to export an ADF from project tango to the sdcard use the code sample from the docs and its failing for me. I'm able to get the export dialog to pop-up successfully with the proper export path. However when I hit the export button, I get a failure in log

12-16 00:48:12.203      192-603/? E/tango_service_library_context﹕ bool RuntimeExportAreaDescription(const string&, const string&, const string&): Internal error occured opening file: /storage/emulated/0/c5617dae-01b0-4825-8ee1-777c17693414
12-16 00:48:12.209    8246-8246/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.projecttango.tango, PID: 8246
com.google.atap.tangoservice.TangoInvalidException
        at com.google.atap.tango.TangoInternal.throwTangoExceptionIfNeeded(TangoInternal.java:118)
        at com.google.atap.tango.TangoInternal.exportAreaDescriptionFile(TangoInternal.java:104)
        at com.google.atap.tango.RequestImportExportActivity.onExportAccepted(RequestImportExportActivity.java:83)
        at com.google.atap.tango.RequestImportExportDialog$1.onClick(RequestImportExportDialog.java:108)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5030)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)

My app has read/write access to external storage which I verified by writing a file with the same path which worked fine.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

This is my export code (which is taken from the tango docs):

        adfToExport = GetAdfs().get(0);

        String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();

        /* this was just a test to verify the app can write to the path
        try {
            String path = externalStorageDirectory + "/c5617dae-01b0-4825-8ee1-777c17693414";
            File file = new File(path);
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            myOutWriter.append("testing 1 2 3");
            myOutWriter.close();
            fOut.close();
        } catch (Exception ex) {
            Log.e(LOG_TAG, "failed to write file: " + path, ex);
        }
        */

        Intent exportIntent = new Intent();
        exportIntent.setClassName(INTENT_CLASSPACKAGE, INTENT_IMPORTEXPORT_CLASSNAME);
        exportIntent.putExtra(EXTRA_KEY_SOURCEUUID, adfToExport);
        exportIntent.putExtra(EXTRA_KEY_DESTINATIONFILE, externalStorageDirectory);
        startActivityForResult(exportIntent, Tango.TANGO_INTENT_ACTIVITYCODE);

Solution

  • Try hardcoding the export path by using code below instead of getting the external storage's path from Environment.getExternalStorageDirectory() :

    String mapsFolder = "/sdcard/maps";
    File file = new File(mapsFolder);
    

    I modified the code from AreaLearningSample for getAppsSpaceADFFolder() function to below and it works fine for me.

    private String getAppSpaceADFFolder() 
    {
    //  String mapsFolder = getFilesDir().getAbsolutePath() + File.separator + "Maps";
        String mapsFolder = "/sdcard/maps";
        File file = new File(mapsFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return mapsFolder;
    }
    

    I am yet to figure out why Environment.getExternalStorageDirectory() doesn't work and throws the above mentioned exception and I'll edit this answer once I have some results.