团队博客5

今天对于获取本地图片的问题进行了处理,其中由于图片过多,加载图片需要很长的时间,经过一些对于代码的修改,我们把图片的缓存时间从将近5秒的时间压缩至1秒多,可以大大减少图片缓存时间。

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);
        }
	}

  

posted @ 2016-09-05 21:29  JYJe族  阅读(138)  评论(0编辑  收藏  举报