BitmapFactory.decodeStream leads to : D/skia: --- Failed to create image decoder with message 'unimplemented'

648 views Asked by At

I'm trying to download an image from a URL by creating a Bitmap using bitmapFactory.decodeStream(InputStream) and then imageView.setImageBitmap(bitmap) but I am always getting this error:

D/skia: --- Failed to create image decoder with message

'unimplemented'. package com.example.flickrapp;

Here is my code:

import statements will go here ...

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
    super.onStart();
    Button b = (Button)findViewById(R.id.getanimage);
    b.setOnClickListener(new GetImageOnClickListener() {
        @Override
        public void onClick(View v) {
            super.onClick(v);
        }
    });
}

public class GetImageOnClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        AsyncFlickrJSONData imagesData = new AsyncFlickrJSONData();
        imagesData.execute("https://www.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json");
    }
}

public class AsyncFlickrJSONData extends AsyncTask<String, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(String... strings) {
        String flickrUrl = strings[0];
        JSONObject jsonFlickr = null;
        URL url = null;
        try {
            url = new URL(flickrUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                String s1 = readStream(in);
                int lengthS = s1.length();
                String s = (String) s1.subSequence(15, lengthS-1);
                jsonFlickr = new JSONObject(s);
            } finally {
                urlConnection.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return jsonFlickr;
    }

    @Override
    protected void onPostExecute(JSONObject jsonFlickr) {
        super.onPostExecute(jsonFlickr);
        try {
            String firstUrl = jsonFlickr.getJSONArray("items").getJSONObject(0).getString("link");
            AsyncBitmapDownloader firstAsyncImage = new AsyncBitmapDownloader();
            firstAsyncImage.execute(firstUrl);
            Log.i("JFL", firstUrl);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String readStream(InputStream in) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            Log.e(TAG, "IOException", e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException", e);
            }
        }
        return sb.toString();
    }
}

public class AsyncBitmapDownloader extends AsyncTask<String, Void, Bitmap> {

    ImageView firstImage = (ImageView) findViewById(R.id.image);

    @Override
    protected Bitmap doInBackground(String... strings) {
        String imageUrl = strings[0];
        Bitmap bm = null;
        URL url = null;
        try {
            url = new URL(imageUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                bm = BitmapFactory.decodeStream(in);
            } finally {
                urlConnection.disconnect();
            }
        } catch(IOException e){
                e.printStackTrace();
        }
        return bm;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        firstImage.setImageBitmap(bitmap);
    }
}

}

Any Suggestions or Ideas are welcomed :)

0

There are 0 answers