/**
*
*/
package com.gmail.charleszq.picorner.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.util.Log;
import com.gmail.charleszq.picorner.model.MediaObject;
import com.nostra13.universalimageloader.core.assist.FlushedInputStream;
/**
* @author Charles(charleszq@gmail.com)
*
*/
public final class ImageUtils {
private static final int IO_BUFFER_SIZE = 8 * 1024;
private static final String TAG = ImageUtils.class.getSimpleName();
public static boolean saveImageToFile(File destFile, Bitmap bitmap) {
if (bitmap == null) {
return false;
}
FileOutputStream fos = null;
try {
if (destFile.exists()) {
destFile.delete();
}
fos = new FileOutputStream(destFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException fnfe) {
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
}
}
}
return true;
}
public static boolean saveImageToFile(Context ctx, String filename,
Bitmap bitmap) {
if (bitmap == null) {
return false;
}
FileOutputStream fos = null;
try {
fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
// will create if not exists
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
return true;
}
/**
* This method must be called in a thread other than UI.
*
* @param url
* @return
*/
public static Bitmap downloadImage(String url) {
// final int IO_BUFFER_SIZE = 4 * 1024;
// AndroidHttpClient is not allowed to be used from the main thread
final HttpClient client = AndroidHttpClient.newInstance("Android"); //$NON-NLS-1$
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode //$NON-NLS-1$//$NON-NLS-2$
+ " while retrieving bitmap from " + url); //$NON-NLS-1$
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
return BitmapFactory.decodeStream(new FlushedInputStream(
inputStream));
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w("I/O error while retrieving bitmap from " + url, e); //$NON-NLS-1$
} catch (IllegalStateException e) {
getRequest.abort();
Log.w("Incorrect URL:" + url, e); //$NON-NLS-1$
} catch (Exception e) {
getRequest.abort();
Log.w("Error while retrieving bitmap from " + url, e); //$NON-NLS-1$
} finally {
if ((client instanceof AndroidHttpClient)) {
((AndroidHttpClient) client).close();
}
}
return null;
}
public static boolean downloadUrlToStream(String urlString,
OutputStream outputStream) {
HttpURLConnection urlConnection = null;
BufferedOutputStream out = null;
BufferedInputStream in = null;
try {
final URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream(),
IO_BUFFER_SIZE);
out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
return true;
} catch (IOException e) {
Log.e(TAG,
"Error in downloadBitmap - " + urlString + " error: " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (final IOException e) {
}
}
return false;
}
public static String getPhotoShareLink(MediaObject photo) {
String url = photo.getLargeUrl();
StringBuilder sb = new StringBuilder();
switch (photo.getMediaSource()) {
case FLICKR:
if (photo.getAuthor() != null) {
sb.append("http://www.flickr.com/photos/"); //$NON-NLS-1$
sb.append(photo.getAuthor().getUserId());
sb.append("/").append(photo.getId()); //$NON-NLS-1$
url = sb.toString();
}
break;
case PX500:
sb.append("http://500px.com/photo/"); //$NON-NLS-1$
sb.append(photo.getId());
url = sb.toString();
break;
case INSTAGRAM:
url = photo.getImageLink();
break;
}
return url;
}
}