2016-08-08 8 views
1

Googleのダンボール用のアプリケーションを開発しようとしていますが、公式SDKからサンプルをダウンロードしました。内部クラスで(スレッド管理するヘルパークラスであるべきである)ImageLoaderTaskUIのスレッドから段ボールサンプルのメソッドを呼び出す必要があります

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スタジオで私にこのエラーが表示されます: メソッドloadImageFromBitmapをUIスレッドから呼び出す必要があります。現在、推定されたスレッドはworkerです。 可能な解決策はありますか?

答えて

2

はonPostExecute内の "loadImageFromBitmap" を移動してみてください:

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); 
     } 
    } 
+0

パーフェクト。バックグラウンドでUI操作を実行できません。ありがとう! – sgrumo

+0

まさに! https://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result) –

0

ええ、バックグラウンドスレッドでloadImageFromBitmapを呼び出さないでください。呼び出すメソッドはdoInBackgroundです。 onPostExecuteメソッドを使用してビットマップをメインスレッドに渡すと、そこで実行できます。

+0

で行うことができますdoInBackgroundに実装したいあなたにも可能なコードを投稿してくださいもらえますか?最後の部分を取得していない! – sgrumo

+0

[OK]を、初めてコードを誤解しました。申し訳ありません。 onPostExecuteメソッドでその呼び出しを行っているようです。メインスレッドにAsyncTaskを作成していますか? – CaseyB

0

あなたはdoInBackground上で実行し、UIを操作することはできません。
onPostExecuteでそのタスクを実行する必要があります。


しかし、それでもあなたは、あなたがこのよう

runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       panoWidgetView.loadImageFromBitmap(BitmapFactory.decodeStream(istr), panoOptions); 
     } 
    }); 
関連する問題