2016-08-08 26 views
1

Versuchen, eine App für Google Pappe zu entwickeln, habe ich die Probe aus dem offiziellen SDK heruntergeladen. In der inneren Klasse ImageLoaderTaskKartonprobe, Methode muss von UI-Thread aufgerufen werden

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 ist das Widget für das Panorama, und es ist in der Aktivität erklärt, die diese innere Klasse umfasst (die es die Hilfsklasse zu verwalten Einfädeln sein soll). Aber Android Studio gibt mir diesen Fehler: Methode loadImageFromBitmap muss aus dem UI-Thread aufgerufen werden, derzeit abgeleitete Thread ist Worker. Jede mögliche Lösung?

Antwort

2

versuchen "loadImageFromBitmap" inside OnPostExecute bewegen:

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

Perfekt. Ich kann keine UI-Operation im Hintergrund ausführen, danke! – sgrumo

+0

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

0

Yeah, rufen Sie nicht loadImageFromBitmap auf dem Hintergrundthread. Die Methode, in der Sie es aufrufen, ist doInBackground. Wenn Sie die Bitmap mithilfe der Methode onPostExecute an den Hauptthread übergeben, können Sie dies dort tun.

+0

Könnten Sie bitte auch den möglichen Code posten? Habe den letzten Teil nicht bekommen! – sgrumo

+0

Ok, ich habe deinen Code beim ersten Mal falsch gelesen. Das tut mir leid. Es sieht so aus, als würden Sie diesen Aufruf in der onPostExecute-Methode ausführen. Erstellen Sie Ihre AsyncTask im Hauptthread? – CaseyB

0

Sie können nicht ausführen und UI-Betrieb auf doInBackground.
Sie sollten diese Aufgabe auf onPostExecute ausführen.


aber immer noch wollen Sie auf doInBackground implementieren, dann können Sie mit dieser Art und Weise tun

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