Android中Firebase Storage URI转Bitmap失败,报错无内容提供者
问题原因
你混淆了两种URI的用法:MediaStore.Images.Media.getBitmap和ImageDecoder.createSource(传入ContentResolver的重载)仅支持本地content://开头的内容URI,而Firebase Storage的getDownloadUrl()返回的是https://开头的网络URL,ContentResolver找不到对应的本地内容提供者,直接触发"No content provider"错误。
解决方案
要将网络URL转成Bitmap,需要先从网络下载图片字节流,再解码为Bitmap,以下是两种可行方案:
方案1:原生网络请求(需处理线程)
网络请求不能在主线程执行,需放到子线程处理,示例代码:
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { new Thread(new Runnable() { @Override public void run() { Bitmap bitmap = null; try { URL url = new URL(uri.toString()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(input); // 回到主线程处理通知图标设置 getActivity().runOnUiThread(new Runnable() { @Override public void run() { // 在这里设置通知图标 } }); } catch (IOException e) { e.printStackTrace(); } } }).start(); } });
方案2:用图片加载库(推荐)
使用Glide或Picasso这类库,自动处理网络请求、线程调度和缓存,代码更简洁。以Glide为例:
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { Glide.with(getContext()) .asBitmap() .load(uri) .into(new CustomTarget<Bitmap>() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { // 在这里拿到Bitmap并设置为通知图标 } @Override public void onLoadCleared(@Nullable Drawable placeholder) { // 资源清理逻辑 } }); } });
注意事项
- 确保App已申请
INTERNET权限(在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET"/>) - 通知图标建议调整到合适尺寸(如24x24dp或48x48dp),避免拉伸变形
- 后台服务中使用Glide时,需用
Glide.with(getApplicationContext())避免内存泄漏
内容的提问来源于stack exchange,提问作者C-Gian
相关产品推荐
相关产品推荐

