0

I'm trying to load a gif from a url to be displayed in an Imageview, store it in the internal storage and then later read it again. But it refuses to either store the image or reading it, not sure which one because I get no exceptions. Loading the image to the imageview works. The first method below (loadImage())

public Bitmap loadImage(String url){
    Bitmap bm = null;
    URL request;
    try {
        if(url!=null){
            request = new URL(url);
            InputStream is = request.openStream();
            bm = BitmapFactory.decodeStream(is);
            is.close();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bm;
}

public String writeGifToInternalStorage (Bitmap outputImage) {
    try {
        String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis());
        ByteBuffer byteBuffer = ByteBuffer.allocate(outputImage.getByteCount());
        outputImage.copyPixelsToBuffer(byteBuffer);
        byteBuffer.flip();
        byte[] data = new byte[byteBuffer.limit()];
        byteBuffer.get(data);
        FileOutputStream fos =  ctx.openFileOutput(fileName, Context.MODE_PRIVATE);
        fos.write(data);
        fos.close();
        return fileName;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public Bitmap readFileFromInternalStorage(String filename) {
    if (filename == null) return null;
        FileInputStream fis;
    try {
        fis = ctx.openFileInput(filename);
        return BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

Any ideas of whats wrong?

just_user
  • 11,769
  • 19
  • 90
  • 135
  • you can't decode like that a raw array of pixels. you need to save, not the pixels from the bitmap, but the file itself. (that is, when you load the file, start by saving it) – njzk2 Dec 05 '13 at 15:36
  • @njzk2 thanks, put that as the answer and I'll mark it as correct! – just_user Dec 05 '13 at 16:09

1 Answers1

2

Your method readFileFromInternalStorage read an encoded image from the file system. This image file should be what you receive from the server.

For that, you need to save the image when you receive it from the server, for example like so:

InputStream is = new BufferedInputStream(request.openStream());
String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis());
FileOutputStream fos = ctx.openFileOutput(fileName, Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int red = 0;
while ((red = is.read(buffer)) != -1) {
    fos.write(buffer,0, red);
}
fos.close();
is.close();

Then, your image is saved to the disk, and you can open it using your readFileFromInternalStorage method.

Also, if you use HttpClient instead of URL, I wrote a one-liner for downloading a file: Android download binary file problems

Community
  • 1
  • 1
njzk2
  • 38,969
  • 7
  • 69
  • 107