VrPanoramaView不显示任何图像

时间:2016-08-25 10:35:13

标签: android android-studio google-cardboard

我正在尝试开发一个简单的VR应用程序,其中我有一个简单的PanoramaView,我将添加一些浮动按钮(稍后)。但目前问题是:我已经按照Android sdk的谷歌教程,但当我尝试在屏幕上运行应用程序是完全黑色。

这是代码:`

public class MainActivity extends Activity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();

    private VrPanoramaView panoWidgetView;
    public boolean loadImageSuccessful;

    //** Tracks the file to be loaded across the lifetime of this app. **/
    private Uri fileUri;
    /** Configuration information for the panorama. **/
    private VrPanoramaView.Options panoOptions = new VrPanoramaView.Options();
    private ImageLoaderTask backgroundImageLoaderTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        panoWidgetView = (VrPanoramaView)findViewById(R.id.pano_view);
        panoWidgetView.setEventListener(new ActivityEventListener());
    }

    protected void onNewIntent(Intent intent) {
        Log.i(LOG_TAG, this.hashCode() + ".onNewIntent()");

        setIntent(intent);

        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        //Controllo che l'intent abbia un file da caricare
        if(Intent.ACTION_VIEW.equals(intent.getAction())) {
            Log.i(LOG_TAG,"ACTION VIEW RECEIVED");
            fileUri = intent.getData();
            if(fileUri == null) {
                Log.w(LOG_TAG, "No data uri specified. Use \"-d /path/filename\".");
            } else {
                Log.i(LOG_TAG, "Using file " + fileUri.toString());
            }
            panoOptions.inputType = intent.getIntExtra("inputType", VrPanoramaView.Options.TYPE_MONO);
            Log.i(LOG_TAG, "Options.inputType = " + panoOptions.inputType);
        } else {
            Log.i(LOG_TAG, "Intent is not ACTION_VIEW. Using default pano image.");
            fileUri = null;
            panoOptions.inputType = VrPanoramaView.Options.TYPE_MONO;
        }

        // Load the bitmap in a background thread to avoid blocking the UI thread

        if (backgroundImageLoaderTask == null) {
            backgroundImageLoaderTask.cancel(true);
        }
        backgroundImageLoaderTask = new ImageLoaderTask();
        backgroundImageLoaderTask.execute(Pair.create(fileUri, panoOptions));
    }
    @Override
    protected void onPause() {
        panoWidgetView.pauseRendering();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        panoWidgetView.resumeRendering();
    }

    @Override
    protected void onDestroy() {
        // Destroy the widget and free memory.
        panoWidgetView.shutdown();

        // The background task has a 5 second timeout so it can potentially stay alive for 5 seconds
        // after the activity is destroyed unless it is explicitly cancelled.
        if (backgroundImageLoaderTask != null) {
            backgroundImageLoaderTask.cancel(true);
        }
        super.onDestroy();
    }
    class ImageLoaderTask extends AsyncTask<Pair<Uri, VrPanoramaView.Options>, Void, Boolean> {
        VrPanoramaView.Options panoOptions = null;  // It's safe to use null VrPanoramaView.Options.
        InputStream istr = null;

        @Override
        protected Boolean doInBackground(Pair<Uri, VrPanoramaView.Options>... information) {

            if(information == null || information.length < 1 || information[0] == null || information[0].first == null) {
                AssetManager assetManager = getAssets();
                try {
                    istr = assetManager.open("andes.jpg");
                    panoOptions = new VrPanoramaView.Options();
                    panoOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;

                } catch (IOException e ) {
                    Log.e(LOG_TAG,"asjdsd"+ e);
                    e.printStackTrace();
                    return false;
                }
            } else {
                try {
                    istr = new FileInputStream(new File(information[0].first.getPath()));
                } catch (FileNotFoundException e) {
                    Log.e(LOG_TAG, "aopsjodiajs" + e );
                    e.printStackTrace();
                }
            }
            try {
                istr.close();
            } catch (IOException e) {
                Log.e(LOG_TAG, "asdasd" + e);
                e.printStackTrace();
                return false;
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            if(istr != null && panoOptions != null ) {
                panoWidgetView.loadImageFromBitmap(BitmapFactory.decodeStream(istr),panoOptions);
            }
            super.onPostExecute(aBoolean);
        }
    }
    private class ActivityEventListener extends VrPanoramaEventListener {

        public void onLoadSuccess() {
            loadImageSuccessful = true;
        }
        @Override
        public void onLoadError(String errorMessage) {
            loadImageSuccessful = false;
            Toast.makeText(
                    MainActivity.this, "Error loading pano: " + errorMessage, Toast.LENGTH_LONG)
                    .show();
            Log.e(LOG_TAG, "Error loading pano: " + errorMessage);
        }
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="sgrumo.realestate">
        <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="24" />
        <uses-permission android:name="android.permission.NFC" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:screenOrientation="landscape">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    <category android:name="com.google.intent.category.CARDBOARD" />
                </intent-filter>
            </activity>
            <activity android:name=".OtherActivity"
                android:screenOrientation="landscape"></activity>
        </application>

    </manifest>

build.gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "sgrumo.realestate"
        minSdkVersion 23
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        jackOptions {
            enabled true
        }
    }
    compileOptions {
        sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_8
        targetCompatibility org.gradle.api.JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile project(':common')
    compile project(':commonwidget')
    compile project(':panowidget')
    compile project(':base')
}

mainActivity代码与Google提供的代码没有什么不同,我仍然无法得到错误的位置。

我的andes.jpg位于资产文件夹中。

1 个答案:

答案 0 :(得分:0)

发现错误。在OnCreate()方法中,我忘了把handleIntent(intent)调用!