Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
    
 Sunami - An Android music player which knows what you want to listen to.
 Copyright (C) 2015 Wojtek Swiderski
    
 Sunami is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 Sunami is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 The GNU General Public License can be found at the root of this repository.
    
 To contact me, email me at wojtek.technology@gmail.com
    
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

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

public class Main {
    public static Bitmap decodeBitmapFromURL(String src, boolean large) {

        // If the artwork returned null, don't want to try to show artwork
        if (src.equals("null")) {
            return null;
        }

        if (large) {
            src = src.replace("large", "t500x500");
        }

        InputStream inputStream = null;
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            inputStream = connection.getInputStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (inputStream == null) {
            return null;
        }

        // Decided not to scale because would have to recreate input stream
        /*
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(inputStream, null, options);
            
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
            
        options.inJustDecodeBounds = false; */
        Bitmap returnBitmap = BitmapFactory.decodeStream(inputStream);
        return returnBitmap;
    }
}