2015-01-21 4 views
0

URL의 이미지를 위젯의 이미지 뷰로로드하려고합니다.위젯의 URL에서 ImageView로드 - android

지금 AsyncTask 클래스를 사용하고 있습니다. 이미지가로드되지 않습니다.

public class MyWidgetIntentReceiver extends BroadcastReceiver { 
    public static int clickCount = -1; 
    private String msg[] = null;  
    private Context con ; 
    RemoteViews remoteViews ; 
    String url = "" ; 
    ImageLoader imageLoader ; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(WidgetUtils.WIDGET_UPDATE_ACTION)) { 

     con = context ; 


      if(clickCount == 0 || clickCount > 10){ 
       //get values 

       getTenLastItems(); 

      }else{ 
       updateWidgetPictureAndButtonListener(context); 
      } 


     } 
    } 

    private void updateWidgetPictureAndButtonListener(Context context) { 
     remoteViews = new RemoteViews(context.getPackageName(), 
       R.layout.widgets_layout); 

     UtilityFunctions utilityFunctions = new UtilityFunctions(); 
     String title = utilityFunctions.getTitleWidget(context, clickCount); 


     // updating view 
     remoteViews.setTextViewText(R.id.txt_view_title_widgets,title); 

     //url = "http://www.shadyab.com/assests/images/upload/yuu1.jpg" ; 


     new MyAsyncTask().execute((Void)null); 



     // re-registering for click listener 
     remoteViews.setOnClickPendingIntent(R.id.sync_button, 
       MyWidgetProvider.buildButtonPendingIntent(context)); 

     MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), 
       remoteViews); 
    } 


    class MyAsyncTask extends AsyncTask<Void, Void, Bitmap> 
    { 
     @Override 
     protected Bitmap doInBackground(Void... urls) { 



      URL url; 
      Bitmap bmp = null ; 
      try { 
       url = new URL("http://www.shadyab.com/assests/images/upload/yuu1.jpg"); 
       bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


      return bmp; 
     } 
     @Override 
     protected void onPostExecute(Bitmap bmp) { 
      super.onPostExecute(bmp); 

      if (bmp != null) { 
       Log.e("error", "is not null"); 
       remoteViews.setImageViewBitmap(R.id.img_view_main_pic_image_widgets,bmp); 
      }else{ 
       Log.e("error", "is null"); 
      } 
     } 
    } 

내 로그 고양이 :

01-21 21:37:10.321: E/error(18618): is not null 

답변

0

여기 예를 참조하십시오 : Get Image from URL

private Bitmap downloadUrl(String strUrl) throws IOException{ 

     Bitmap bitmap=null; 
     InputStream iStream = null; 
     try{ 
      URL url = new URL(strUrl); 
      /** Creating an http connection to communcate with url */ 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      /** Connecting to url */ 
      urlConnection.connect(); 

      /** Reading data from url */ 
      iStream = urlConnection.getInputStream(); 

      /** Creating a bitmap from the stream returned from the url */ 
      bitmap = BitmapFactory.decodeStream(iStream); 

     }catch(Exception e){ 
      Log.d("Exception while downloading url", e.toString()); 
     }finally{ 
      iStream.close(); 
     } 
     return bitmap; 
} 

당신이 필요로하는 경우 뭔가를 더 강력한 모습

관련 문제