Android Open Source - JiangHomeStyle_Android_Phone Loading Image Tools






From Project

Back to project page JiangHomeStyle_Android_Phone.

License

The source code is released under:

Apache License

If you think the Android project JiangHomeStyle_Android_Phone 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.cidesign.jianghomestylephone.tools;
// ww w  .j ava 2s  .co m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

public class LoadingImageTools
{
  public void loadingImage(ImageView view, String path)
  {
    new AsyncLoadImage(view).execute(path);
  }

  class AsyncLoadImage extends AsyncTask<String, Void, Bitmap>
  {
    private ImageView view;

    public AsyncLoadImage(ImageView view)
    {
      this.view = view;
    }

    @Override
    protected void onPreExecute()
    {
      // ???????????
      super.onPreExecute();
    }

    @Override
    protected Bitmap doInBackground(String... params)
    {
      /*
      Bitmap bm = null;
      File file = new File(params[0]);
      InputStream is;
      try
      {
        is = new FileInputStream(file);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = 5; // width??hight????????????
        options.inTempStorage = new byte[1024 * 1024]; // 12k
        bm = BitmapFactory.decodeStream(is, null, options);

      }
      catch (FileNotFoundException e)
      {
        e.printStackTrace();
      }
      catch (java.lang.OutOfMemoryError e)
      {
        e.printStackTrace();
      }
      */
      
      return getImageBitmap(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap bm)
    {
      if (bm != null && view != null)
      {
        view.setImageBitmap(bm);
      }
    }
  }

  public void loadingNativeImage(Activity ctx, ImageView view, String path)
  {
    new AsyncLoadNativeImage(ctx, view).execute(path);
  }

  class AsyncLoadNativeImage extends AsyncTask<String, Void, Bitmap>
  {
    private Activity ctx;
    private ImageView view;

    public AsyncLoadNativeImage(Activity ctx, ImageView view)
    {
      this.ctx = ctx;
      this.view = view;
    }

    @Override
    protected Bitmap doInBackground(String... params)
    {
      try
      {
        InputStream is = ctx.getAssets().open(params[0]);
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = 3;
        options.inTempStorage = new byte[12 * 1024]; // 12k
        bm = BitmapFactory.decodeStream(is, null, options);

        return bm;
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
      catch (java.lang.OutOfMemoryError e)
      {
        e.printStackTrace();
      }
      return null;
    }

    @Override
    protected void onPostExecute(Bitmap bm)
    {
      if (bm != null && view != null)
      {
        view.setImageBitmap(bm);
      }
    }
  }

  public static Bitmap getImageBitmap(String path)
  {
    // Allocate files and objects outside of timingoops
    File file = new File(path);
    RandomAccessFile in = null;
    Bitmap bmp = null;
    try
    {
      in = new RandomAccessFile(file, "rws");
      final FileChannel channel = in.getChannel();
      final int fileSize = (int) channel.size();
      final byte[] testBytes = new byte[fileSize];
      final ByteBuffer buff = ByteBuffer.allocate(fileSize);
      final byte[] buffArray = buff.array();
      @SuppressWarnings("unused")
      final int buffBase = buff.arrayOffset();

      channel.position(0);
      channel.read(buff);
      buff.flip();
      buff.get(testBytes);
      bmp = Bitmap_process(buffArray);
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (java.lang.OutOfMemoryError e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (in != null)
        {
          in.close();
        }
        
      }
      catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    
    return bmp;
  }

  public static Bitmap Bitmap_process(byte[] buffArray)
  {
    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inDither = false; // Disable Dithering mode
    options.inPurgeable = true; // Tell to gc that whether it needs free
                  // memory, the Bitmap can be cleared
    options.inInputShareable = true; // Which kind of reference will be used
                      // to recover the Bitmap data after
                      // being clear, when it will be used
                      // in the future
    options.inTempStorage = new byte[32 * 1024]; // Allocate some temporal
                            // memory for decoding

    options.inSampleSize = 7;

    Bitmap imageBitmap = BitmapFactory.decodeByteArray(buffArray, 0, buffArray.length, options);
    return imageBitmap;
  }
  
  public static Bitmap loadingNative(Context ctx,String path)
  {
    InputStream is = null;
    Bitmap bm = null;
    try
    {
      is = ctx.getAssets().open(path);
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = false;
      options.inSampleSize = 3;
      options.inTempStorage = new byte[12 * 1024]; // 12k
      bm = BitmapFactory.decodeStream(is, null, options);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }    
    return bm;
  }
}




Java Source Code List

com.cidesign.jianghomestylephone.DetailActivity.java
com.cidesign.jianghomestylephone.JiangActivity.java
com.cidesign.jianghomestylephone.MainActivity.java
com.cidesign.jianghomestylephone.SplashActivity.java
com.cidesign.jianghomestylephone.adapter.CommunityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.HumanityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LandscapeViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LayoutCaculateAdapter.java
com.cidesign.jianghomestylephone.adapter.StoryViewpagerAdapter.java
com.cidesign.jianghomestylephone.async.AsyncDownTask.java
com.cidesign.jianghomestylephone.async.AsyncInitCommunityData.java
com.cidesign.jianghomestylephone.async.AsyncInitData.java
com.cidesign.jianghomestylephone.async.AsyncInitHomeData.java
com.cidesign.jianghomestylephone.async.AsyncInitHumanityData.java
com.cidesign.jianghomestylephone.async.AsyncInitLandscapeData.java
com.cidesign.jianghomestylephone.async.AsyncInitStoryData.java
com.cidesign.jianghomestylephone.db.DatabaseConfigUtil.java
com.cidesign.jianghomestylephone.db.DatabaseHelper.java
com.cidesign.jianghomestylephone.entity.ArticleEntity.java
com.cidesign.jianghomestylephone.entity.FileListEntity.java
com.cidesign.jianghomestylephone.entity.LayoutEntity.java
com.cidesign.jianghomestylephone.entity.RelativeLayoutRulesEntity.java
com.cidesign.jianghomestylephone.http.ArticalOperation.java
com.cidesign.jianghomestylephone.http.DownLoadThread.java
com.cidesign.jianghomestylephone.service.DownloadService.java
com.cidesign.jianghomestylephone.tools.CategoryDataLoadingLogic.java
com.cidesign.jianghomestylephone.tools.FileOperationTools.java
com.cidesign.jianghomestylephone.tools.JiangCategory.java
com.cidesign.jianghomestylephone.tools.LayoutMarginSetting.java
com.cidesign.jianghomestylephone.tools.LoadingDataFromDB.java
com.cidesign.jianghomestylephone.tools.LoadingImageTools.java
com.cidesign.jianghomestylephone.tools.MD5Tools.java
com.cidesign.jianghomestylephone.tools.StorageUtils.java
com.cidesign.jianghomestylephone.tools.TimeTools.java
com.cidesign.jianghomestylephone.tools.WidgetCache.java
com.cidesign.jianghomestylephone.tools.XmlParseTools.java
com.cidesign.jianghomestylephone.version.NetworkTool.java
com.cidesign.jianghomestylephone.version.VersionConfig.java
com.cidesign.jianghomestylephone.version.VersionUpdate.java
com.cidesign.jianghomestylephone.widget.CommunityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.CustomScrollView.java
com.cidesign.jianghomestylephone.widget.HScrollViewTouchLogic.java
com.cidesign.jianghomestylephone.widget.HumanityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.LandscapeRelativeLayout.java
com.cidesign.jianghomestylephone.widget.PopMenu.java
com.cidesign.jianghomestylephone.widget.StoryRelativeLayout.java
org.apache.tools.zip.AbstractUnicodeExtraField.java
org.apache.tools.zip.AsiExtraField.java
org.apache.tools.zip.CentralDirectoryParsingZipExtraField.java
org.apache.tools.zip.ExtraFieldUtils.java
org.apache.tools.zip.FallbackZipEncoding.java
org.apache.tools.zip.GeneralPurposeBit.java
org.apache.tools.zip.JarMarker.java
org.apache.tools.zip.NioZipEncoding.java
org.apache.tools.zip.Simple8BitZipEncoding.java
org.apache.tools.zip.UnicodeCommentExtraField.java
org.apache.tools.zip.UnicodePathExtraField.java
org.apache.tools.zip.UnixStat.java
org.apache.tools.zip.UnparseableExtraFieldData.java
org.apache.tools.zip.UnrecognizedExtraField.java
org.apache.tools.zip.UnsupportedZipFeatureException.java
org.apache.tools.zip.Zip64ExtendedInformationExtraField.java
org.apache.tools.zip.Zip64Mode.java
org.apache.tools.zip.Zip64RequiredException.java
org.apache.tools.zip.ZipConstants.java
org.apache.tools.zip.ZipEightByteInteger.java
org.apache.tools.zip.ZipEncodingHelper.java
org.apache.tools.zip.ZipEncoding.java
org.apache.tools.zip.ZipEntry.java
org.apache.tools.zip.ZipExtraField.java
org.apache.tools.zip.ZipFile.java
org.apache.tools.zip.ZipLong.java
org.apache.tools.zip.ZipOutputStream.java
org.apache.tools.zip.ZipShort.java
org.apache.tools.zip.ZipUtil.java