在Widget中将图像设置为背景

时间:2014-03-17 06:35:47

标签: android bitmap android-asynctask widget

我正在使用Widget我想将Image(来自服务器)设置为widget background.but图像未显示。我正在使用Asynktask女巫我从服务器获取位图。代码中没有异常,但我不知道主要问题是什么。请建议我解决这个问题。

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.e("Nomi1", imageView + "");
    // path = Uri.parse("android.resource://"+context.getPackageName()+"/" +
    // R.drawable.ic_media_pause);

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

    Intent configIntent = new Intent(context, VideoListActivity.class);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
    new ReadingJson().execute();
    // remoteViews.setImageViewUri(R.id.widget_img, uri);
    remoteViews.setOnClickPendingIntent(R.id.widget_img, configPendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

private class ReadingJson extends AsyncTask<String, Void, Boolean> {



    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Boolean doInBackground(String... params) {
        bitmap = getBitmapFromURL(urlPath);
        Log.e("bitmap", bitmap + "");
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        remoteViews.setImageViewBitmap(R.id.widget_img, bitmap);
        if (bitmap!=null) {
            Log.e("bitmap onpost", bitmap+ "");
        }
    }
}

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

0 个答案:

没有答案