0

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

我找到了使用内容提供程序在本地加载图像的代码示例:

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 加载位图作为背景图像?

4

1 回答 1

-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);
于 2016-05-02T16:29:37.680 回答