0

I am building app for TV and trying to change background image on home screen when recommendation card is selected.

I've found code samples where image is loaded locally with content provider:

public static class RecommendationBackgroundContentProvider extends ContentProvider {

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    /*
     * content provider serving files that are saved locally when recommendations are built
     */
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        Log.i(TAG, "openFile");
        int backgroundId = Integer.parseInt(uri.getLastPathSegment());
        File bitmapFile = getNotificationBackground(getContext(), backgroundId);
        return ParcelFileDescriptor.open(bitmapFile, ParcelFileDescriptor.MODE_READ_ONLY);
    }
}

The problem with this is that I need file to be loaded from URL and not locally. I don't see any other useful methods in Notification.Builder class. What should I use to load Bitmap from URL as a background image?

Astagron
  • 277
  • 4
  • 13

1 Answers1

-1

Create this in your class:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView iView;

    public DownloadImageTask(ImageView iView) {
        this.iView = iView;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mImage = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mImage = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mImage;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

and then call it with:

String YOUR_URL_STRING = "http://www.image.com/image.jpg";

new DownloadImageTask((ImageView) rowView.findViewById(R.id.iv_preview))
                .execute(YOUR_URL_STRING);
Milos Lulic
  • 627
  • 5
  • 22
  • Thanks but I think this doesn't help in my case. I need to change background image on Home screen on TV platform when recommendation card is selected. – Astagron May 02 '16 at 16:35
  • DownloadImageTask class do convert Url to Bitmap. You can use any View istead of ImageView, just change ImageView to your View. – Milos Lulic May 02 '16 at 16:46