0

I have a uri of image from firebase but i don't know how to load it into bitmap, because i needed to cut the four corner edge of the image.

I tried it with resource id by providing drawable icon and it does worked fine, but not working from uri

Below is my code:

        uri imagefile = model.getImageUri();

        if (imagefile !=null){

        imageView.setVisibility(View.VISIBLE);


        Resources res = c.getResources();

        //How i'm loading the image
        Bitmap src = BitmapFactory.decodeResource(res, 
        Integer.parseInt(imagefile)); 


        RoundedBitmapDrawable dr =
        RoundedBitmapDrawableFactory.create(res, src);
        dr.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 
        30.0f);
        imageView.setImageDrawable(dr);

    }

How to load image using uri? It will help me to solve other related issues as well. Thanks

user861594
  • 5,733
  • 3
  • 29
  • 45

2 Answers2

1

I suggest you use AsyncTask to keep the process at the background so as to not lag the UI:

ImageLoadAsyncTask.java

public class ImageLoadAsyncTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadAsyncTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);
    }
}

Then, use this codeto load your image from Firebase:

ImageLoadAsyncTask imageLoadAsyncTask = new ImageLoadAsyncTask(url, imageView);
 imageLoadAsyncTask.execute();

Good Luck!

khalid3e
  • 399
  • 3
  • 14
0

you can check how to convert Uri to Url in this (watch the answer of CommonsWare)

How to convert android.net.Uri to java.net.URL?

And this it how to load bitmap from URL (watch the answer of rajath)

Load image from url

tainguyen
  • 85
  • 1
  • 14
  • Thanks. I tried his answer, but i got error because the code is running on main thread. and i looked down for another answer, another answer improved his own, but i don't how to use it with my code. –  Jul 09 '18 at 18:19