Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedInputStream;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL;

import android.util.Log;

public class Main {
    private static final String TAG = "WareNinjaUtils";

    public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent",
                System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK");

        // determine the image size and allocate a buffer
        int fileSize = connection.getContentLength();
        byte[] imageData = new byte[fileSize];

        // download the file
        Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")");

        if (fileSize > 0) {

            BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
            int bytesRead = 0;
            int offset = 0;
            while (bytesRead != -1 && offset < fileSize) {
                bytesRead = istream.read(imageData, offset, fileSize - offset);
                offset += bytesRead;
            }

            istream.close();
        } else
            Log.d(TAG, "fileSize is 0! skipping");

        // clean up
        connection.disconnect();

        return imageData;
    }
}