Android Bitmap 防止OOM及其他操作

本文介绍了一种有效防止内存溢出(OOM)的方法,通过压缩Bitmap图片大小来优化资源使用。同时,阐述了如何根据时间筛选特定命名的图片,并提供了一套完整的文件操作流程。重点在于提高图片处理效率及资源管理能力。


防止OOM,即压缩Bitmap图片

// 压缩图片大小
	public Bitmap reBitmap(String path) {

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = false;
		// 使图片大小为原来的1/4
		options.inSampleSize = 4;
		Bitmap bitmap = BitmapFactory.decodeFile(path, options);
		return bitmap;

	}

文件的重命名

File file1=new File(path);  
	File file2=new File(pastePath);  
	boolean flag = file1.renameTo(file2);

根据时间选取以时间命名的图片

// 返回图片路径
	public ArrayList<String> GetFilePath(Long commitTime) {
		// File file_ = new File(isSdcard() + photoPath);
		File file_ = new File(photoPath);
		File[] files = file_.listFiles();
		ArrayList<String> myFilePath = new ArrayList<String>();
		if (files != null) {
			Log.v("Camera", "进入判断");
			int count = files.length;
			System.out.println(count);
			for (int i = 0; i < count; i++) {
				File file = files[i];
				String filepath = file.getAbsolutePath(); // 得到路径
				Log.v("路径1---", filepath);
				
				// pathTime为拍照时间,此判断用来选择一次任务的照片
				Long pathTime = Long.parseLong(filepath.substring(
						photoPath.length(), photoPath.length()
								+ commitTime.toString().length()));
				
				if (commitTime - pathTime <= 0) {

					if (filepath.endsWith("jpg") || filepath.endsWith("gif")
							|| filepath.endsWith("bmp")
							|| filepath.endsWith("png")) {

						// 把路径存入容器中
						
						myFilePath.add(filepath);
						Log.v("路径3---", filepath);
						System.out.println("size is " + myFilePath.size());
					}

				}

			}
		}
		return myFilePath;
	}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值