首页 文章

从Firebase存储下载图像并将其保存在SD卡中时出现问题

提问于
浏览
0

我已成功将图像上传到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

2 回答

  • 0

    使用Glide试试吧 .

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

    其中 mDownloadUrl 是您的图片网址 .

  • 1

    Firebase存储没有已注册的内容解析程序 . 您获得的下载网址实际上是一个普通的https:// Url,您可以将其输入Glide .

    您也可以直接下载此Url . 看看这个question .

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

相关问题