Below is code for loading large image from url in android mobile.
Below is code for loading large image from url in android mobile.
public class ImageLoadActivity extends Activity {
ImageView mImageView;
BitmapWorkTask mBitmapWorkTask;
public static final String URL="http://www.iiccentre.org/image/Restaurants.jpg";
public static final String URL2="http://nssdc.gsfc.nasa.gov/planetary/image/cassini_titan_15.jpg";
ProgressDialog mProgressDialog;
public static int deviceHeight;
public static int deviceWidth;
DisplayMetrics dm;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_load);
displayIntialize();
mImageView=(ImageView)findViewById(R.id.img_loaded);
mBitmapWorkTask=new BitmapWorkTask();
mBitmapWorkTask.execute(URL,URL2);
}
private void displayIntialize(){
dm= new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
deviceHeight=dm.heightPixels;
deviceWidth=dm.widthPixels;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_image_load, menu);
return true;
}
public class BitmapWorkTask extends AsyncTask{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog=new ProgressDialog(ImageLoadActivity.this, ProgressDialog.STYLE_SPINNER);
mProgressDialog=ProgressDialog.show(ImageLoadActivity.this, "Please wait", "While image is loading");
mProgressDialog.show();
}
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result!=null){
mImageView.setImageBitmap(result);
}
if(mProgressDialog!=null){
mProgressDialog.dismiss();
}
}
@Override protected Bitmap doInBackground(String... params) {
Bitmap mBitmap=null;
int size=params.length;
String urlIs=params[1];
mBitmap=downloadBitmap(urlIs);
return mBitmap;
}
}
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
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 + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(inputStream);
bis.mark(1024 * 1024);
final Bitmap bitmap = decodeSampledBitmapFromResource(bis,deviceWidth,deviceHeight);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) { // Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
//Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, e.toString());
Log.e("LOG_TAG", "Error while retrieving bitmap from" + url + e.toString());
} finally {
if (client != null) {
client.close();
}
} return null;
}
public static Bitmap decodeSampledBitmapFromResource(BufferedInputStream inputStream,int reqWidth, int reqHeight) { // First decode with
//inJustDecodeBounds=true
//to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
try {
inputStream.reset();
} catch (IOException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream(inputStream, null, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
} return inSampleSize;
}
}
}
}
}
Below is code for loading large image from url in android mobile.
public class ImageLoadActivity extends Activity {
ImageView mImageView;
BitmapWorkTask mBitmapWorkTask;
public static final String URL="http://www.iiccentre.org/image/Restaurants.jpg";
public static final String URL2="http://nssdc.gsfc.nasa.gov/planetary/image/cassini_titan_15.jpg";
ProgressDialog mProgressDialog;
public static int deviceHeight;
public static int deviceWidth;
DisplayMetrics dm;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_load);
displayIntialize();
mImageView=(ImageView)findViewById(R.id.img_loaded);
mBitmapWorkTask=new BitmapWorkTask();
mBitmapWorkTask.execute(URL,URL2);
}
private void displayIntialize(){
dm= new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
deviceHeight=dm.heightPixels;
deviceWidth=dm.widthPixels;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_image_load, menu);
return true;
}
public class BitmapWorkTask extends AsyncTask{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog=new ProgressDialog(ImageLoadActivity.this, ProgressDialog.STYLE_SPINNER);
mProgressDialog=ProgressDialog.show(ImageLoadActivity.this, "Please wait", "While image is loading");
mProgressDialog.show();
}
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result!=null){
mImageView.setImageBitmap(result);
}
if(mProgressDialog!=null){
mProgressDialog.dismiss();
}
}
@Override protected Bitmap doInBackground(String... params) {
Bitmap mBitmap=null;
int size=params.length;
String urlIs=params[1];
mBitmap=downloadBitmap(urlIs);
return mBitmap;
}
}
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
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 + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(inputStream);
bis.mark(1024 * 1024);
final Bitmap bitmap = decodeSampledBitmapFromResource(bis,deviceWidth,deviceHeight);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) { // Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
//Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, e.toString());
Log.e("LOG_TAG", "Error while retrieving bitmap from" + url + e.toString());
} finally {
if (client != null) {
client.close();
}
} return null;
}
public static Bitmap decodeSampledBitmapFromResource(BufferedInputStream inputStream,int reqWidth, int reqHeight) { // First decode with
//inJustDecodeBounds=true
//to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
try {
inputStream.reset();
} catch (IOException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream(inputStream, null, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
} return inSampleSize;
}
}
}
}
}
Here is output: