2017-11-22 40 views
0

写真を含むサーバーから情報を取得しようとしています。 ServerManagerというクラスを作成しました。希望の情報をダウンロードすることができます。しかし、私はjava.lang.IllegalStateException: Method call should happen from the main thread.が得られるので、ピカソで画像をダウンロードすることはできません。Android Picasso:メインスレッドからメソッド呼び出しが発生する

これは私がServerManagerでやっているものです:

// Check if the photo is already downloaded 
if (!checkInternalPhoto(member)) { 
    final String outputURL = context.getFilesDir().getAbsolutePath() + "/" + member.photoPath; 
    String photoURL = rootURL + "photos/" + member.photoPath; 
    Picasso.with(context) 
      .load(photoURL) 
      .into(new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        File file = new File(outputURL); 
        try { 
         file.createNewFile(); 
         FileOutputStream outputStream = new FileOutputStream(file); 
         bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream); 
         outputStream.flush(); 
         outputStream.close(); 
        } catch (IOException e) { 
         Log.e("IOException",e.getLocalizedMessage()); 
        } 
       } 
       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 
       } 
       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 
       } 
    }); 
} 

私は何ができますか?

編集:あなたが作成したものとは別のスレッドからUI要素を更新することはできません

public void downloadPhotos(List<Member> memberList){ 
    String rootInURL = serverManager.getRootURL(); 
    String rootOutURL = serverManager.getOutputURL(); 
    for(int i = 0; i < memberList.size(); i++){ 
     if(!serverManager.checkInternalPhoto(memberList.get(i))){ 
      final String outputURL = rootOutURL + memberList.get(i).photoPath; 
      String photoURL = rootInURL + "photos/" + memberList.get(i).photoPath; 
      Picasso.with(this) 
        .load(photoURL) 
        .into(new Target() { 
         @Override 
         public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
          File file = new File(outputURL); 
          try{ 
           file.createNewFile(); 
           FileOutputStream outputStream = new FileOutputStream(file); 
           bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream); 
           outputStream.flush(); 
           outputStream.close(); 
           Toast.makeText(context, "YEpa: " ,Toast.LENGTH_LONG).show(); 
          } catch (IOException e){ 
           Toast.makeText(context, "Error: " + e.toString(),Toast.LENGTH_LONG).show(); 
          } 
         } 

         @Override 
         public void onBitmapFailed(Drawable errorDrawable) { 

         } 

         @Override 
         public void onPrepareLoad(Drawable placeHolderDrawable) { 

         } 
        }); 
     } 
    } 
} 
+0

ここからあなたは 'downloadPhotos()'を呼び出しています。 'MainActivity'コードで答えを更新してください – samirk433

答えて

1

...私は私のMainActivityのコードを使用しようと私は同じエラーメッセージを取得していますそのui要素。 Androidではほぼ常にメインスレッドを意味します。

あなたがする必要があるのは、photoUrl文字列をメインスレッドのアクティビティまたはフラグメントに返信することです。これを行う伝統的な方法は、Handlerを使用することです。これについてもっと読むことができます:https://developer.android.com/training/multiple-threads/communicate-ui.html

+0

私はこれを理解していないと思います... 私の主な活動でコードを実行しようとしましたが、同じエラーメッセージが表示されます:( (私は記事を編集しました) –

+0

'downloadPhotos'とは何ですか? – jbarat

関連する問題