Android在小部件图像中显示图像

时间:2014-02-11 13:30:07

标签: android android-widget

我成功创建了小部件,现在我想下载图像并使用小部件图像。这是我的代码,但我无法在小部件中显示我的下载图像。

    RemoteViews views; 
    int appWidgetId; 
    public File file; 
    public Bitmap bitmap; 

    @Override
    public void onReceive(Context context, Intent intent) { 
        super.onReceive(context, intent); 
        if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) { 
            Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show(); 
        } 
    } 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
            int[] appWidgetIds) { 
        final int N = appWidgetIds.length; 
        for (int i = 0; i < N; i++) { 
            appWidgetId = appWidgetIds[i]; 

            Intent intent = new Intent(context, MainActivity.class); 
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
                    intent, 0); 

            views = new RemoteViews(context.getPackageName(), 
                    R.layout.widget_demo); 
            String aa = (MainActivity.itemList.get(0) 
                    .get(MainActivity.KEY_title)).toString(); 

            views.setOnClickPendingIntent(R.id.widgetPic, pendingIntent); 

            views.setOnClickPendingIntent(R.id.widgetDesc, pendingIntent); 
            views.setTextViewText(R.id.widgetDesc, aa); 
            String ab = (MainActivity.itemList.get(0) 
                    .get(MainActivity.KEY_image)).toString(); 

            // views.setImageViewUri(R.id.widgetPic, Uri.parse(ab)); 
            new DownloadTask().execute("http://api.androidhive.info/images/sample.jpg"); 

            // views.setImageViewBitmap( 
            // R.id.widgetPic, 
            // ((BitmapDrawable) context.getResources().getDrawable( 
            // R.drawable.aaaaaaaaaaaaaaa)).getBitmap()); 

            appWidgetManager.updateAppWidget(appWidgetId, views); 
        } 
    } 

    public void updateAppWidget(Context context, 
            AppWidgetManager appWidgetManager, int appWidgetId) { 
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), 
                R.layout.widget_demo); 
        try { 

        } catch (Exception e) { 

        } 

        appWidgetManager.updateAppWidget(appWidgetId, updateViews); 
    } 

    private Bitmap downloadUrl(String strUrl) throws IOException { 
        Bitmap bitmap = null; 
        InputStream iStream = null; 
        try { 
            URL url = new URL(strUrl); 

            HttpURLConnection urlConnection = (HttpURLConnection) url 
                    .openConnection(); 
            urlConnection.connect(); 
            iStream = urlConnection.getInputStream(); 

            bitmap = BitmapFactory.decodeStream(iStream); 

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

        return bitmap; 
    } 

    private class DownloadTask extends AsyncTask<String, Integer, Bitmap> { 
        Bitmap bitmap = null; 

        @Override
        protected Bitmap doInBackground(String... url) { 
            try { 
                bitmap = downloadUrl(url[0]); 
            } catch (Exception e) { 
                Log.d("Background Task", e.toString()); 
            } 
            return bitmap; 
        } 

        @Override
        protected void onPostExecute(Bitmap result) { 

            views.setImageViewBitmap(R.id.widgetPic, result); 


            // Toast.makeText(getBaseContext(), 
            // "Image downloaded successfully",Toast.LENGTH_SHORT).show(); 
        } 

    } 

}

如何使用setImageViewUri显示此图像?如果有人知道解决方案,请帮助我。感谢

1 个答案:

答案 0 :(得分:0)

完成下载某些图像后,必须通过调用updateAppWidget()来刷新应用程序小部件。 也许是这样,

    @Override
    protected void onPostExecute(Bitmap result) { 

        views.setImageViewBitmap(R.id.widgetPic, result);
        updateAppWidget(appWidgetId, remoteView);
    } 

您可以在创建时将appWidgetId和remoteView变量传递给DownloadTask。