Java tutorial
/******************************************************************************* * Copyright 2011-2013 Sergey Tarasevich * * 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. *******************************************************************************/ package com.jobs.lib_v1.imageloader.core.download; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URLConnection; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.content.ContentResolver; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import com.jobs.lib_v1.app.AppException; import com.jobs.lib_v1.imageloader.core.DisplayImageOptions; import com.jobs.lib_v1.net.http.DataHttpClient; import com.jobs.lib_v1.net.http.DataHttpHeader; /** * Provides retrieving of {@link InputStream} of image by URI from network or file system or app resources.<br /> * {@link URLConnection} is used to retrieve image stream from network. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @see HttpClientImageDownloader * @since 1.8.0 */ public class BaseImageDownloader implements ImageDownloader { /** {@value} */ protected static final int BUFFER_SIZE = 32 * 1024; // 32 Kb private static final String ERROR_UNSUPPORTED_SCHEME = "UIL doesn't support scheme(protocol) by default [%s]. " + "You should implement this support yourself (BaseImageDownloader.getStreamFromOtherSource(...))"; protected final Context context; public BaseImageDownloader(Context context) { this.context = context.getApplicationContext(); } public BaseImageDownloader(Context context, int connectTimeout, int readTimeout) { this.context = context.getApplicationContext(); } @Override public InputStream getStream(String imageUri, Object extra) throws IOException { try { switch (Scheme.ofUri(imageUri)) { case HTTP: case HTTPS: return getStreamFromNetwork(imageUri, extra); case FILE: return getStreamFromFile(imageUri, extra); case CONTENT: return getStreamFromContent(imageUri, extra); case ASSETS: return getStreamFromAssets(imageUri, extra); case DRAWABLE: return getStreamFromDrawable(imageUri, extra); case UNKNOWN: default: return getStreamFromOtherSource(imageUri, extra); } } catch (Throwable e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(AppException.getErrorString(e, "Unkown IO Exception.")); } } } /** * Retrieves {@link InputStream} of image by URI (image is located in the network). * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for * URL. */ protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException { DefaultHttpClient client = DataHttpClient.buildClient(); HttpGet request = new HttpGet(imageUri); DataHttpHeader.initRequestHeader(request, null); return client.execute(request).getEntity().getContent(); } /** * Retrieves {@link InputStream} of image by URI (image is located on the local file system or SD card). * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws IOException if some I/O error occurs reading from file system */ protected InputStream getStreamFromFile(String imageUri, Object extra) throws IOException { String filePath = Scheme.FILE.crop(imageUri); return new BufferedInputStream(new FileInputStream(filePath), BUFFER_SIZE); } /** * Retrieves {@link InputStream} of image by URI (image is accessed using {@link ContentResolver}). * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws FileNotFoundException if the provided URI could not be opened */ protected InputStream getStreamFromContent(String imageUri, Object extra) throws FileNotFoundException { ContentResolver res = context.getContentResolver(); Uri uri = Uri.parse(imageUri); return res.openInputStream(uri); } /** * Retrieves {@link InputStream} of image by URI (image is located in assets of application). * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws IOException if some I/O error occurs file reading */ protected InputStream getStreamFromAssets(String imageUri, Object extra) throws IOException { String filePath = Scheme.ASSETS.crop(imageUri); return context.getAssets().open(filePath); } /** * Retrieves {@link InputStream} of image by URI (image is located in drawable resources of application). * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image */ protected InputStream getStreamFromDrawable(String imageUri, Object extra) { String drawableIdString = Scheme.DRAWABLE.crop(imageUri); int drawableId = Integer.parseInt(drawableIdString); BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(drawableId); Bitmap bitmap = drawable.getBitmap(); ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, os); return new ByteArrayInputStream(os.toByteArray()); } /** * Retrieves {@link InputStream} of image by URI from other source with unsupported scheme. Should be overriden by * successors to implement image downloading from special sources.<br /> * This method is called only if image URI has unsupported scheme. Throws {@link UnsupportedOperationException} by * default. * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws IOException if some I/O error occurs * @throws UnsupportedOperationException if image URI has unsupported scheme(protocol) */ protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException { throw new UnsupportedOperationException(String.format(ERROR_UNSUPPORTED_SCHEME, imageUri)); } }