Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.net.Uri;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Converts the uri to an InputStream.
     *
     * @param context Context used to resolve file and content.
     * @param uri     Uri to convert.
     * @return InputStream or null if something went wrong.
     */
    private static InputStream toInputStream(Context context, Uri uri) {
        String scheme = uri.getScheme();
        if (scheme.equals("http") || scheme.equals("https")) {
            try {
                return new URL(uri.getPath()).openConnection().getInputStream();
            } catch (NullPointerException e) {
                return null;
            } catch (MalformedURLException e) {
                return null;
            } catch (IOException e) {
                return null;
            }
        } else if (scheme.equals("file") || scheme.equals("content")) {
            try {
                return context.getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException e) {
                return null;
            }
        }
        return null;
    }
}