Android Open Source - SwitchTabs Utils






From Project

Back to project page SwitchTabs.

License

The source code is released under:

Apache License

If you think the Android project SwitchTabs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.normal.testdemo.utils;
//  w  ww  . ja v a2s .c  o m
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * Created by ex_chenjinghao on 2014/4/8.
 * ???
 */
public class Utils {

  // ???????????????
  public static File cacheDir;

  // ??Url???????????????????????????
  // ????????????????????
  public static Bitmap getBitmapByUrl(String url) {
    String fileName = getFileNameFromUrl(url);
    Bitmap bitmap = null;
    // ??????????
    bitmap = getBitmapFromCache(fileName);
    // ????????????
    if (bitmap == null) {
      URL imgUrl = null;
      InputStream is = null;
      try {
        imgUrl = new URL(url);
        is = imgUrl.openStream();
        // ??????
        bitmap = BitmapFactory.decodeStream(is);
        // ??????
        saveBitmapToCache(bitmap, fileName);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      finally {
        try {
          is.close();
        }
        catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return bitmap;
  }

  // ????????????????????null
  public static Bitmap getBitmapFromCache(String fileName) {
    Bitmap result = null;
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(new File(cacheDir, fileName));
      result = BitmapFactory.decodeStream(fis);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      if (fis != null) {
        try {
          fis.close();
        }
        catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return result;
  }

  // ????????????
  public static void saveBitmapToCache(Bitmap bitmap, String fileName) {
    if (bitmap == null) {
      return;
    }
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(new File(cacheDir, fileName));
      // ????????
      bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      if (fos != null) {
        try {
          fos.close();
        }
        catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  // ?Url??????????
  public static String getFileNameFromUrl(String src) {
    int i = src.lastIndexOf("/");
    return src.substring(i + 1, src.length());
  }
}




Java Source Code List

com.normal.testdemo.activitys.MainActivity.java
com.normal.testdemo.activitys.SplashActivity.java
com.normal.testdemo.adapters.HotelListAdapter.java
com.normal.testdemo.adapters.SectionsPagerAdapter.java
com.normal.testdemo.beans.Hotel.java
com.normal.testdemo.beans.Hotels.java
com.normal.testdemo.fragments.PlaceholderFragment.java
com.normal.testdemo.utils.AsynImageLoader.java
com.normal.testdemo.utils.Utils.java
com.normal.testdemo.view.XListViewFooter.java
com.normal.testdemo.view.XListViewHeader.java
com.normal.testdemo.view.XListView.java