必须从UI线程调用Cardboard示例,方法

时间:2016-08-08 13:52:03

标签: android multithreading android-studio google-cardboard

尝试为google纸板开发应用程序,我从官方sdk下载了样本。在内部类ImageLoaderTask(它应该是管理线程的辅助类)

    class ImageLoaderTask extends AsyncTask<Pair<Uri, Options>, Void, Boolean> {

    /**
     * Reads the bitmap from disk in the background and waits until it's loaded by pano widget.
     */
    @Override
    protected Boolean doInBackground(Pair<Uri, Options>... fileInformation) {
      Options panoOptions = null;  // It's safe to use null VrPanoramaView.Options.
      InputStream istr = null;
      if (fileInformation == null || fileInformation.length < 1
          || fileInformation[0] == null || fileInformation[0].first == null) {
        AssetManager assetManager = getAssets();
        try {
          istr = assetManager.open("andes.jpg");
          panoOptions = new Options();
          panoOptions.inputType = Options.TYPE_STEREO_OVER_UNDER;
        } catch (IOException e) {
          Log.e(TAG, "Could not decode default bitmap: " + e);
          return false;
        }
      } else {
        try {
          istr = new FileInputStream(new File(fileInformation[0].first.getPath()));
          panoOptions = fileInformation[0].second;
        } catch (IOException e) {
          Log.e(TAG, "Could not load file: " + e);
          return false;
        }
      }

      panoWidgetView.loadImageFromBitmap(BitmapFactory.decodeStream(istr), panoOptions);
      try {
        istr.close();
      } catch (IOException e) {
        Log.e(TAG, "Could not close input stream: " + e);
      }

      return true;
    }
  }

panoWidgetView是全景图的小部件,它在包含此内部类的活动内声明。 但是Android Studio给了我这个错误: 必须从UI线程调用方法loadImageFromBitmap,当前推断的线程是worker。 任何可能的解决方

3 个答案:

答案 0 :(得分:2)

尝试移动&#34; loadImageFromBitmap&#34;在onPostExecute中:

class ImageLoaderTask extends AsyncTask<Pair<Uri, BitmapFactory.Options>, Void, Boolean> {
        Options panoOptions = null;  // It's safe to use null VrPanoramaView.Options.
        InputStream istr = null;

        /**
         * Reads the bitmap from disk in the background and waits until it's loaded by pano widget.
         */
        @Override
        protected Boolean doInBackground(Pair<Uri, BitmapFactory.Options>... fileInformation) {

            if (fileInformation == null || fileInformation.length < 1
                    || fileInformation[0] == null || fileInformation[0].first == null) {
                AssetManager assetManager = getAssets();
                try {
                    istr = assetManager.open("andes.jpg");
                    panoOptions = new Options();
                    panoOptions.inputType = Options.TYPE_STEREO_OVER_UNDER;
                } catch (IOException e) {
                    Log.e(TAG, "Could not decode default bitmap: " + e);
                    return false;
                }
            } else {
                try {
                    istr = new FileInputStream(new File(fileInformation[0].first.getPath()));
                    panoOptions = fileInformation[0].second;
                } catch (IOException e) {
                    Log.e(TAG, "Could not load file: " + e);
                    return false;
                }
            }

            try {
                istr.close();
            } catch (IOException e) {
                Log.e(TAG, "Could not close input stream: " + e);
            }

            return true;
        }

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

答案 1 :(得分:0)

是的,不要在后台线程上调用loadImageFromBitmap。你调用它的方法是doInBackground。如果使用onPostExecute方法将位图传递给主线程,则可以在那里执行。

答案 2 :(得分:0)

您无法在doInBackground上执行和UI操作。
你应该在onPostExecute上执行该任务。


但是你仍然希望在doInBackground上实现,那么你可以这样做

  runOnUiThread(new Runnable() {
            @Override
            public void run() {
                panoWidgetView.loadImageFromBitmap(BitmapFactory.decodeStream(istr), panoOptions); 
        }
    });