0

I'm facing this issue only with this url always BitmapFactory.decodeStream failed to decode bufferedInputStream

I think the problem is actually from connection request but It's been two days and I'm trying to fix the issue..

Any simple help would be very helpful..

My code:

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> Log.d(getClass().getName(), "OkHttp: " + message));
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.addInterceptor(logging);

    Request request = new Request.Builder().url("https://manga.ae/cdn/cdn1/2338/0029/15.jpg")
            .addHeader("Referer","https://mngaar.com/")
            .addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36")
            .addHeader("Content-Type", "image/jpeg")
            .build();

    client.build().newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.i("Tag", "error" + e.getMessage());
        }

        @Override
        public void onResponse(Call call, final Response response) throws IOException {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ResponseBody in = response.body();
                    InputStream inputStream = in.byteStream();
                    Log.i("inputStream", "inputstream value = " + inputStream);
                    try {
                        Log.i("responseBody", "ResponseBody value = " + in.string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                    Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
                    Log.i("bitmap", "bitmap value = " + bitmap);
                    imageView.setImageBitmap(bitmap);

                }
            });

        }
    });

LogCat:

OkHttp: <-- 200 https://manga.ae/cdn/cdn1/2338/0029/15.jpg (1497ms)
OkHttp: date: Thu, 15 Jul 2021 11:11:10 GMT
    OkHttp: content-type: image/jpeg
    OkHttp: content-length: 144200
    OkHttp: x-frame-options: SAMEORIGIN
    OkHttp: last-modified: Fri, 23 Dec 2016 15:02:29 GMT
    OkHttp: etag: "23348-54454acb46b40"
    OkHttp: x-xss-protection: 1; mode=block
    OkHttp: x-frame-options: SAMEORIGIN
    OkHttp: x-content-type-options: nosniff
    OkHttp: cache-control: max-age=259200
    OkHttp: cf-cache-status: HIT
    OkHttp: age: 5953
    OkHttp: accept-ranges: bytes
D/com.example.newbagdad.MainActivity: OkHttp: expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    OkHttp: report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=2hhpsuHQMTAPG04De4208d2yzHTbJnY5dki9YYGFECczz%2FlOcs9mxR9jX79eq9UOlxUuNAupDpYzPdOX0YCmytNbNI9K1ALuBjt7616iUwvuAhvJE%2B6QLvpG0g%3D%3D"}],"group":"cf-nel","max_age":604800}
    OkHttp: nel: {"report_to":"cf-nel","max_age":604800}
    OkHttp: vary: Accept-Encoding
    OkHttp: server: cloudflare
    OkHttp: cf-ray: 66f28c6d391b082c-CDG
    OkHttp: alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400
D/com.example.newbagdad.MainActivity: OkHttp: 
    OkHttp: <-- END HTTP (binary 144200-byte body omitted)
I/inputStream: inputstream value = 144200
D/skia: --- codec->getAndroidPixels() failed.
I/bitmap: bitmap value = null

midou
  • 75
  • 1
  • 5
  • Have you considered using an image-loading library, such as Glide or Picasso? – CommonsWare Jul 15 '21 at 12:04
  • Without `BufferedInputStream`? – Darkman Jul 15 '21 at 12:41
  • @CommonsWare yes i used glide, picasso and even Android Universal Image Loader but almost the same result.. – midou Jul 15 '21 at 15:14
  • @Darkman yes it's same thing.. – midou Jul 15 '21 at 15:17
  • 1
    That implies there is something strange with the server and how it is responding to the request, or that the data returned is not a valid JPEG image. – CommonsWare Jul 15 '21 at 15:57
  • `in.string()` this function is risk, it will try to load your response into memory and cause some side effect. Try to turn off the log and give it a try. – NamNH Jul 16 '21 at 04:24
  • 1
    @NamNH yes `in.string()` was just a mistake and i removed it, i just wanted to make sure the picture is in ResponseBody.. I have tried this library [PRDownloader](https://github.com/MindorksOpenSource/PRDownloader) like @CommonsWare said it returned not valid image.. i just confirmed that the problem is with the server response.. – midou Jul 16 '21 at 17:33

0 Answers0