Question

I have a android app to load multiple images from mysql database onto a ImageButton.

imageButton.setImageBitmap(fetchBitmap("http://www...~.jpg"));

I was once able to load png successfully but it also fails now (No success with jpg images ever). Here is the code I use for downloading images:-

public static Bitmap fetchBitmap(String urlstr) {
    InputStream is= null;
    Bitmap bm= null;
    try{
        HttpGet httpRequest = new HttpGet(urlstr);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        is = bufHttpEntity.getContent();
        BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
        bm = BitmapFactory.decodeStream(is);
    }catch ( MalformedURLException e ){
        Log.d( "RemoteImageHandler", "Invalid URL: " + urlstr );
    }catch ( IOException e ){
        Log.d( "RemoteImageHandler", "IO exception: " + e );
    }finally{
        if(is!=null)try{
            is.close();
        }catch(IOException e){}
    }
    return bm;
} 

I get this error:-

D/skia(4965): --- SkImageDecoder::Factory returned null

I have already tried various combinations as suggested here, here and several other solutions, but it doesnt work for me. Am I missing something? Image is definitely present in the web address I enter.

Thank you.

Was it helpful?

Solution 2

The problem was that the images could not be downloaded because the directory in which the images were kept did not have "execute" permission. As soon as the permission was added, the app works smoothly :)

OTHER TIPS

Use below code for download image and store into bitmap, it may help you.

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top