Shunan
Shunan

Reputation: 3254

Issue in downloading image from Firebase Storage and saving it in SD Card

I had uploaded the image on Firebase Storage successfully. I have the URI and using Glide, I'm able to show the image on an ImageView. I want to save this image on my SD card but I'm getting an exception

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

In here:

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

Here is my complete code:

@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 looks like this:

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

Upvotes: 0

Views: 1236

Answers (2)

Shailendra Kushwah
Shailendra Kushwah

Reputation: 415

Using Glide Try this.

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

SaveImage(bitmap);

where mDownloadUrl is your image URL.

Upvotes: 1

Benjamin Wulfe
Benjamin Wulfe

Reputation: 1735

Firebase Storage does not have a registered content resolver. The download Url you get is actually a plain vanilla https:// Url that you can feed into Glide.

You can also download this Url directly. Check out this question.

Just call downloadUri.toString() to get the download Url in string form.

Upvotes: 0

Related Questions