setting Image as background in Widget

烂漫一生 提交于 2019-12-25 02:39:13

问题


I am Working on Widget i want to set Image (from server) as widget background.but Image is not shown. I am using Asynktask in witch i am fetching bitmap from the server. there is no Exception in code but I don't know whats the main problem. Kindly suggest me any solution about that.

    @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;
    }
}

来源:https://stackoverflow.com/questions/22448242/setting-image-as-background-in-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!