Java tutorial
//package com.java2s; /* * PanoramaGL library * Version 0.1 * Copyright (c) 2010 Javier Baez <javbaezga@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Rect; public class Main { /**resource methods*/ public static Bitmap getBitmap(Context context, String url) { Bitmap bitmap = null; InputStream is = null; try { url = url.trim(); if (url.indexOf("res://") == 0) { int sepPos = url.lastIndexOf("/"); int resId = context.getResources().getIdentifier(url.substring(sepPos + 1), url.substring(6, sepPos), context.getPackageName()); is = context.getResources().openRawResource(resId); } else if (url.indexOf("file://") == 0) { File file = new File(url.substring(7)); if (file.canRead()) is = new FileInputStream(file); } BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = true; bitmap = BitmapFactory.decodeStream(is, new Rect(), options); return bitmap; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } } } } public static Bitmap getBitmap(Context context, int resId) { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = true; InputStream is = context.getResources().openRawResource(resId); Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options); is.close(); return bitmap; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } } }