选择推荐卡时更改背景图像

时间:2016-05-02 15:56:56

标签: android android-tv leanback

我正在构建电视应用,并在选择推荐卡时尝试在主屏幕上更改背景图像。

我找到了代码示例,其中图片是通过内容提供商在本地加载的:

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

这个问题是我需要从URL而不是本地加载文件。我在Notification.Builder课程中没有看到任何其他有用的方法。我应该使用什么来从URL加载Bitmap作为背景图像?

1 个答案:

答案 0 :(得分:-1)

在班级中创建:

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

然后用:

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

new DownloadImageTask((ImageView) rowView.findViewById(R.id.iv_preview))
                .execute(YOUR_URL_STRING);