Quantcast

load image from web

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

load image from web

John Murray

Can anyone help me here

I realise I’ve got to get the image from a stream and have found this bit of Java code which I am struggling to translate into c#/mono

I know there’s no equivalent of ‘final’ in c#

But its not just that there’s no buffereinput streams and buffereoutput streams

Can’t help thinking it could be simplified

 

This is the java code

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);
        copy(in, out);
        out.flush();

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

        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;
}

 

 

TIA

John Murray


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: load image from web

Jonathan Pryor
On May 4, 2011, at 1:09 PM, John Murray wrote:
> Can’t help thinking it could be simplified

It can be drastically simplified [0]. There is a BitmapFactory.DecodeStream() method [1], and URL.OpenStream() [2] returns a Stream:

        var bitmap = BitmapFactory.DecodeStream (new Java.Net.URL(url).OpenStream());

Alternatively, you could use BitmapFactory.DecodeByteArray() with System.Net.WebClient.DownloadData(), or BitmapFactory.DownloadStream() with WebClient.OpenRead(), or...

 - Jon

[0] The same could be done in Java, too:

        bitmap = BitmapFactory.decodeStream(new URL(url).openStream());

I have no idea why they felt the need to download the entire file as a byte[] in the first place...

[1] http://docs.mono-android.net/Android.Graphics.BitmapFactory.DecodeStream%20(System.IO.Stream)
[2] http://docs.mono-android.net/Java.Net.URL.OpenStream%20()

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: load image from web

Bozo Ivkovic
Would this approach work with google static map tiles?
i.e getting image from url: "http://maps.google.com/maps/api/staticmap?center=Zagreb+Croatia&size=178x200&maptype=mobile&sensor=false&zoom=15"

Sorry for being lazy and not trying my self... but I'll be glad to have confirmation/negation from first hand...

Regards,
Bozo

On Wed, May 4, 2011 at 22:07, Jonathan Pryor <[hidden email]> wrote:
On May 4, 2011, at 1:09 PM, John Murray wrote:
> Can’t help thinking it could be simplified

It can be drastically simplified [0]. There is a BitmapFactory.DecodeStream() method [1], and URL.OpenStream() [2] returns a Stream:

       var bitmap = BitmapFactory.DecodeStream (new Java.Net.URL(url).OpenStream());

Alternatively, you could use BitmapFactory.DecodeByteArray() with System.Net.WebClient.DownloadData(), or BitmapFactory.DownloadStream() with WebClient.OpenRead(), or...

 - Jon

[0] The same could be done in Java, too:

       bitmap = BitmapFactory.decodeStream(new URL(url).openStream());

I have no idea why they felt the need to download the entire file as a byte[] in the first place...

[1] http://docs.mono-android.net/Android.Graphics.BitmapFactory.DecodeStream%20(System.IO.Stream)
[2] http://docs.mono-android.net/Java.Net.URL.OpenStream%20()

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: load image from web

csdlee
In reply to this post by John Murray
            Bitmap bitmap=null;
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.Method = "GET";
            req.BeginGetResponse(new AsyncCallback((IAsyncResult iar) =>
            {
                HttpWebRequest reqr = (HttpWebRequest)iar.AsyncState;
                HttpWebResponse respr = (HttpWebResponse)req.EndGetResponse(iar);
               
                BufferedStream bs=new BufferedStream(respr.GetResponseStream());
                BitmapFactory.Options options = new BitmapFactory.Options();  
                options.InJustDecodeBounds = false;  
                options.InPurgeable = true;  
                options.InDither = false;  
                options.InPreferredConfig = Bitmap.Config.Rgb565;  
                bitmap = BitmapFactory.DecodeStream(bs, null, options);
            }
Loading...