从小部件中的URL加载ImageView - android

时间:2015-01-21 18:05:28

标签: android android-widget

我想将URL中的图像加载到窗口小部件中的imageview中。

现在,我正在使用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

1 个答案:

答案 0 :(得分: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;
}

如果你需要更健壮的外观