【问题标题】:Issue in downloading image from Firebase Storage and saving it in SD Card从 Firebase 存储下载图像并将其保存在 SD 卡中的问题
【发布时间】:2017-01-14 01:55:50
【问题描述】:

我已成功将图像上传到 Firebase 存储。我有 URI 并使用 Glide,我可以在 ImageView 上显示图像。我想将此图像保存在我的 SD 卡上,但出现异常

java.io.FileNotFoundException: No content provider:
https://firebasestorage.googleapis.com/..

在这里:

    try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), myUri);
            SaveImage(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

这是我的完整代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_pic);

        Intent intent = getIntent();
        String str = intent.getStringExtra("pic");
        Uri myUri = Uri.parse(str);
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), myUri);
            SaveImage(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImageView imageView = (ImageView)findViewById(R.id.displayPic);

        Glide.with(getApplicationContext()).load(myUri)
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }
    private void SaveImage(Bitmap finalBitmap) {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

URI 如下所示:

https://firebasestorage.googleapis.com/example.appspot.com/o/pics%2Fc8742c7e-8f59-4ba3-bf6f-12aadfdf4a.jpg?alt=media&token=9bsdf67d-f623-4bcf-95d7-5ed97ecf1a21

【问题讨论】:

  • I have the uri 你没有向我们展示它的样子。你认为这无关紧要吗?
  • @greenapps,我已经编辑了我的问题。
  • 好的。好吧,您可以先评论自己的异常消息。它告诉你什么?
  • 提示:MediaStore.Images.Media.getBitmap() 旨在从……MediaStore 获取位图。您认为来源在哪里?
  • 你能告诉我一个合适的函数吗?

标签: android firebase android-glide firebase-storage


【解决方案1】:

使用 Glide 试试这个。

Bitmap bitmap= Glide.
        with(this).
        load(mDownloadUrl).
        asBitmap().
        into(100, 100). // Width and height
        get();

SaveImage(bitmap);

mDownloadUrl 是您的图片网址。

【讨论】:

    【解决方案2】:

    Firebase 存储没有注册的内容解析器。你得到的下载 URL 实际上是一个普通的 https:// URL,你可以将它输入到 Glide 中。

    您也可以直接下载此网址。看看这个question。

    只需调用 downloadUri.toString() 即可获取字符串形式的下载 Url。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多