團隊博客5

今天對於獲取本地圖片的問題進行了處理,其中因爲圖片過多,加載圖片須要很長的時間,通過一些對於代碼的修改,咱們把圖片的緩存時間從將近5秒的時間壓縮至1秒多,能夠大大減小圖片緩存時間。java

package com.example.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.example.shuxing.AppInfor;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;


public class AppInforService {
	
	private Context context;
	private PackageManager pm;
	public AppInforService(Context context)
	{
		this.context=context;
		pm=context.getPackageManager();
	}
	
	/*獲得手機中全部的應用程序信息*/
	public List<AppInfor> getAppInfor() throws Exception
	{
		//建立要返回的集合對象
		List<AppInfor> appInfor =new ArrayList<AppInfor>();
		//獲取手機中全部安裝的應用集合
		List<ApplicationInfo> applicationInfor=pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		//遍歷全部的應用集合
		for(ApplicationInfo info : applicationInfor)
		{
			if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0)  
	        {  
				AppInfor appInfo=new AppInfor();
				
				//獲取應用程序的圖標
				Drawable app_icon=info.loadIcon(pm);
				appInfo.setIcon(app_icon);
				
				//獲取應用名稱
				String app_name=info.loadLabel(pm).toString();
				appInfo.setName(app_name);
				
				//獲取應用的包名
				String packageName=info.packageName;
				appInfo.setPackageName(packageName);
				
				//獲取版本信息
				try
				{
					PackageInfo packageInfo=pm.getPackageInfo(packageName, 0);
					String appversion=packageInfo.versionName;
					appInfo.setAppversion(appversion);
				}catch(NameNotFoundException e){
					e.printStackTrace();
				}
				//獲取路徑
				String path=info.sourceDir;
				appInfo.setPath(path);
				
				//更新顯示當前包得大小信息
				String dir=info.publicSourceDir;
		        int value=Integer.valueOf((int)new File(dir).length());
		        BigDecimal apkSize=parseApkSize(value);
		        String size=apkSize.toString();
		        appInfo.setSize(size);
				appInfor.add(appInfo);
	        }
			
			
		}
		return appInfor;
	}
	private BigDecimal parseApkSize(int size) {
	    BigDecimal bd = new BigDecimal((double)size/(1024*1024));
	                BigDecimal setScale = bd.setScale(2, BigDecimal.ROUND_DOWN);
	    return setScale;
	}
}

  

package com.example.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.example.shuxing.PictureInfor;
import com.example.shuxing.PicturePath;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;

import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;

public class PictureInforService {

	private Context context;
	private List<PictureInfor> list=new ArrayList<PictureInfor>();
	private ArrayList<PicturePath> listpath;
	public PictureInforService(Context context)
	{
		this.context=context;
	}

	public List<PictureInfor> getPictureInfor()
	{
		MyPicture mp=new MyPicture();
		listpath=mp.doInBackground();
		
		if(listpath.size()==0)
		{
			return null;			
		}

		for(int i=0;i<listpath.size();i++)
		{
			File file=new File(listpath.get(i).getPath());
			PictureInfor picture=new PictureInfor();
			//圖片名稱
			picture.setName(file.getName());
			//圖片路徑
			picture.setPath(file.getAbsolutePath());
			
			//圖片大小
	        int value=Integer.valueOf((int)file.length());
	        BigDecimal pSize=parseApkSize(value);
	        String size=pSize.toString();
			picture.setSize(size);
			
			
			//圖片
			Bitmap bitmap=MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), listpath.get(i).getID(), Thumbnails.MICRO_KIND, null);
			picture.setIcon(bitmap);
			
			//圖片日期
			String date=null;
		    try{
			     ExifInterface exif = new ExifInterface(file.getPath());
			     date=exif.getAttribute(ExifInterface.TAG_DATETIME);
			}
			catch(Exception ee){
			}
			picture.setData(date);
			
			list.add(picture);
			
		}
		return list;
	}
	private BigDecimal parseApkSize(int size) {
	    BigDecimal bd = new BigDecimal((double)size/(1024*1024));
	                BigDecimal setScale = bd.setScale(3, BigDecimal.ROUND_DOWN);
	    return setScale;
	}

  

public class MyPicture extends AsyncTask<String, Integer, ArrayList<PicturePath>> {

        @Override
        protected ArrayList<PicturePath> doInBackground(String... params) {
        	ArrayList<PicturePath> lpath=new ArrayList<PicturePath>();
			String[] proj={MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID};
            Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            ContentResolver mContentResolver =context.getContentResolver();
			Cursor cursor=mContentResolver.query(mImageUri, proj, null, null,null);
			while(cursor.moveToNext())
			{
				PicturePath pp=new PicturePath();
				String path=cursor.getString(0);
				long id=Long.parseLong(cursor.getString(1));
				
				pp.setID(id);
				pp.setPath(path);
				lpath.add(pp);
			}
			/*
			 Uri mImageUri1 = MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI;
	            ContentResolver mContentResolver1 =context.getContentResolver();
				Cursor cursor1=mContentResolver1.query(mImageUri1,proj, null, null, null);
			Cursor cursor1=context.getContentResolver().query(uri1, proj1, null, null, null);
			while(cursor1.moveToNext())
			{
				String path=cursor1.getString(0);
				lpath.add(new File(path).getAbsolutePath());
			}*/
			cursor.close();
			/*cursor1.close();*/
			return lpath;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(ArrayList<PicturePath> bytes) {
            super.onPostExecute(bytes);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
	}
相關文章
相關標籤/搜索