package org.wikimedia.commons;

import java.net.*;
import java.io.*;

import android.content.Context;
import android.graphics.*;
import android.net.Uri;
import android.os.*;
import android.provider.MediaStore;
import android.widget.*;

class ImageLoaderTask extends AsyncTask<Uri, String, Bitmap> {
    ImageView view;

    private Bitmap getBitmap(Uri url) throws MalformedURLException, IOException {
        if(url.getScheme().equals("content")) {
            return MediaStore.Images.Media.getBitmap(view.getContext().getContentResolver(), url);
        } else {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url.toString()).getContent());
            return bitmap;
        }
    }

    ImageLoaderTask(ImageView view) {
        this.view = view;
    }

    @Override
    protected Bitmap doInBackground(Uri... params) {
        Uri url = params[0];
        Bitmap bitmap;
        try {
            bitmap = getBitmap(url);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        view.setImageBitmap(result);
    }
}

@@ -203,16 +203,76 @@
 aStore;%0A
+import android.util.Log;%0Aimport android.view.WindowManager;%0A
 import a
@@ -382,73 +382,650 @@
    p
-rivate Bitmap getBitmap(Uri url) throws MalformedURLException, IO
+ublic int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) %7B%0A        // Raw height and width of image%0A        final int height = options.outHeight;%0A        final int width = options.outWidth;%0A        int inSampleSize = 1;%0A%0A        if (height %3E reqHeight %7C%7C width %3E reqWidth) %7B%0A            if (width %3E height) %7B%0A                inSampleSize = Math.round((float) height / (float) reqHeight);%0A            %7D else %7B%0A                inSampleSize = Math.round((float) width / (float) reqWidth);%0A            %7D%0A        %7D%0A        return inSampleSize;%0A    %7D%0A%0A    public Bitmap getBitmap(Uri imageUri) throws FileNotFound
 Exce
@@ -1032,24 +1032,25 @@
 ption %7B%0A
+%0A
         
 if(url.g
@@ -1045,155 +1045,596 @@
     
-if(url.getScheme().equals(%22content%22)) %7B%0A            return MediaStore.Images.Media.getBitmap(view.getContext().getContentResolver(), url
+WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);%0A        int reqHeight = wm.getDefaultDisplay().getHeight();%0A        int reqWidth = wm.getDefaultDisplay().getWidth();%0A        %0A        // First decode with inJustDecodeBounds=true to check dimensions%0A        final BitmapFactory.Options options = new BitmapFactory.Options();%0A        options.inJustDecodeBounds = true;%0A        InputStream bitmapStream = view.getContext().getContentResolver().openInputStream(imageUri);%0A        BitmapFactory.decodeStream(bitmapStream, null, options
 );%0A
+%0A
         
 %7D el
@@ -1633,161 +1633,422 @@
     
-%7D else %7B%0A            B
+// Calculate inSampleSize%0A        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);%0A        %0A        // Decode b
 itmap 
-bitmap = BitmapFactory.decodeStream((InputStream) new URL(url.toString()).getContent());%0A            return bitmap;%0A        %7D
+with inSampleSize set%0A        options.inJustDecodeBounds = false;%0A        %0A        // Re-get the InputStream!%0A        bitmapStream = view.getContext().getContentResolver().openInputStream(imageUri);%0A        return BitmapFactory.decodeStream(bitmapStream, null, options);
 %0A   
@@ -2296,95 +2296,8 @@
 l);%0A
-        %7D catch (MalformedURLException e) %7B%0A            throw new RuntimeException(e);%0A
     

Fix bug causing larger image sizes to not load