Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.net.Uri;

public class Main {
    /**
     * Generate an input stream reading from an android URI
     * 
     * @param uri
     * @return
     * @throws IOException
     */
    public static InputStream getFromURI(Context context, Uri uri) throws IOException {

        if (uri.getScheme().equals("content"))
            return context.getContentResolver().openInputStream(uri);
        else if (uri.getScheme().equals("file")) {
            URL url = new URL(uri.toString());

            return url.openStream();
        } else {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(uri.toString());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null)
                return entity.getContent();
            else
                throw new IOException("No HTTP response");
            // Use the regular java stuff
            // URL url = new URL(uri.toString());

            // return url.openStream();
        }
    }
}