Java tutorial
//package com.java2s; /* * Ubuntu One Files - access Ubuntu One cloud storage on Android platform. * * Copyright 2011-2012 Canonical Ltd. * * This file is part of Ubuntu One Files. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see http://www.gnu.org/licenses */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Main { /** * See: http://stackoverflow.com/questions/477572/android-strange * -out-of-memory-issue-while-loading-an-image * -to-a-bitmap-object/823966#823966 * Thanks to StackOverflow user named Fedor. */ public static Bitmap decodeFile(File f, int size) { Bitmap b = null; try { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); int scale = 1; if (o.outHeight > size || o.outWidth > size) { scale = (int) Math.pow(2.0, (int) Math .round(Math.log(size / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inTempStorage = new byte[32 * 1024]; o2.inPurgeable = true; o2.inSampleSize = scale; fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { } return b; } }