0

I want to load a image from a url and show it on an image view. The image is bigger than the image view and has a good resolution. But the image shown in the view is very blurry. The image is not that blur if I enlarge the image view. So I think its a scaling problem. I want to load the images dynamically, so I don't won't to download them. I tried several solutions from this question here: Bad image quality after resizing/scaling bitmap and also this one: How to scale an Image in ImageView to keep the aspect ratio but nothing worked. This is my code, for loading the image into the view:

Bitmap bm1 = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());

runOnUiThread(() -> {
    ((ImageView) findViewById(R.id.simImg1)).setImageBitmap(bm1);
 
});

This is the image view

            <ImageView
                android:id="@+id/simImg1"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:padding="5dp" />

This is the url for the Image: https://www.ingolstadt.de/media/custom/3052_32_1_g.JPG

This is what it looks like: Screenshot

Thank you in advance!

Marco
  • 248
  • 3
  • 11

2 Answers2

0

Use Glide instead of fetching the image yourself. It resizes the image respecting the image view size.

Add its dependency in your build.gradle:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}

And in java code:

Glide.with(context)
    .load(url)
    .into(imageView);
aminography
  • 21,986
  • 13
  • 70
  • 74
  • I implemented glide, but the images are still very blurry – Marco Jul 27 '20 at 13:26
  • Post xml layout, please. If possible, share the image url also. – aminography Jul 27 '20 at 13:27
  • I've tested it. It's no blurry at all. Do you want to set the size of the image view exactly 100dp x 200dp? because the image view is portrait but the image you want to show is landscape and this is not a good idea. – aminography Jul 28 '20 at 08:17
  • You are right, I set the width to wrap content and only the height to 200dp. But it's still blurry – Marco Jul 28 '20 at 09:13
  • If you set the width to `wrap_content`, set `android:adjustViewBounds="true"` also. It'd be great if you take a screenshot then share it with me. – aminography Jul 28 '20 at 09:15
  • I shared the screenshot in the question. Just for your info, we are using the app on a android minix tv box :) – Marco Jul 28 '20 at 09:32
  • Thank you Marco. As I see, it is not blurry, its quality is exactly equal with the original image. – aminography Jul 28 '20 at 09:39
  • Ok, yes its acceptable now, thank you for this. But this image here https://static.zara.net/photos///2020/I/0/1/p/5520/067/400/2/w/1140/5520067400_6_1_1.jpg?ts=1594831649343 is still very blurry – Marco Jul 29 '20 at 08:24
  • I think the main problem is in the size of ImageView and the policy of how you show the images. For example, you can fix the size of ImageViews and then show images as `centerCrop`. In this way, all images will be shown in a certain aspect ratio and they will be treated equally. In the end, I strongly recommend trusting glide. It loads images with no trouble and everything you see in ImageView is the best possible thing. – aminography Jul 29 '20 at 08:33
0

Without using any library use this method to download original image and show it

 // DownloadImage AsyncTask
        private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
    
            }
    
            @Override
            protected Bitmap doInBackground(String... URL) {
    
                String imageURL = URL[0];
    
                Bitmap bitmap = null;
                try {
                    // Download Image from URL
                    InputStream input = new java.net.URL(imageURL).openStream();
                    // Decode Bitmap
                    bitmap = BitmapFactory.decodeStream(input);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return bitmap;
            }
    
            @Override
            protected void onPostExecute(Bitmap result) {
    
                    if (result != null) {
                        File destination = new File(getActivity().getCacheDir(),
                                "image" + ".jpg");
                        try {
                            destination.createNewFile();
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            result.compress(Bitmap.CompressFormat.PNG, 100 /*ignored for PNG*/, bos);
                            byte[] bitmapdata = bos.toByteArray();
    
                            FileOutputStream fos = new FileOutputStream(destination);
                            fos.write(bitmapdata);
                            fos.flush();
                            fos.close();
                            selectedFile = destination;
                            runOnUiThread(() -> {
    ((ImageView) findViewById(R.id.simImg1)).setImageBitmap(result);});
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

   }

How to use it?

new DownloadImage().execute("url_here);
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • Thank your for your answer, but I have to change the images dynamically if the user clicks on a button. So I don't won't to download the images. – Marco Jul 27 '20 at 13:24
  • I also tried your suggestion, just to see if it works, but the image still has a very bad quality – Marco Jul 28 '20 at 08:07