android把内存卡中的图片或者其他的文件转存在其他的路径中

这是一个比较简单的问题,但是也是我们经常回遇到的问题,就是在Android的开发过程中,如何将内存卡的图片或者其他的文件转存,然后对这个文件进行处理,因为我们不能对原文件进行处理,最近正在做图片方面的android项目,所以用到了这方面的知识,就和大家分享一下吧。


private void string2File() {
		tempFiles = new File[resultFileList.size()];
		passFileMap = new HashMap<String, File>();
		for (int i = 0; i < resultFileList.size(); ++i) {
			String name = resultFileList.get(i).substring(
					resultFileList.get(i).lastIndexOf("/") + 1);
			name = getCacheDir(mContext) + "/" + name;
			File file = new File(resultFileList.get(i));
			tempFiles[i] = new File(name);
			Uri uri = Uri.fromFile(tempFiles[i]);
			try {
				Bitmap bitmap = decodeFile(file, 1000);
				if (!tempFiles[i].exists()) {
					tempFiles[i].createNewFile();
				}
				FileOutputStream out = new FileOutputStream(tempFiles[i]);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);
				out.flush();
				out.close();
				passFileMap.put(tempFiles[i].getAbsolutePath(), tempFiles[i]);
				// tempFiles[i].delete();
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	}

	private Bitmap decodeFile(File f, int bmpsize) {
		if (f == null || !f.exists())
			return null;

		try {
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			o.inPreferredConfig = Bitmap.Config.ARGB_8888;
			o.inInputShareable = true;
			o.inPurgeable = true;
			BitmapFactory.decodeStream(new FileInputStream(f), null, o);
			final int REQUIRED_SIZE = bmpsize;
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			if (width_tmp > REQUIRED_SIZE || height_tmp > REQUIRED_SIZE) {
				if (width_tmp > height_tmp) {
					scale = width_tmp / REQUIRED_SIZE;
				} else {
					scale = height_tmp / REQUIRED_SIZE;
				}
			}
			// decode with inSampleSize

			BitmapFactory.Options o2 = new BitmapFactory.Options();

			o2.inSampleSize = scale;
			o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
			o2.inInputShareable = true;
			o2.inPurgeable = true;

			Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
					null, o2);
			return bmp;
		} catch (FileNotFoundException e) {

		}

		return null;

	}

我们是先取出原文件,对他进行特定的处理,例如图片的话,可以进行压缩什么的,然后将处理之后的图片存放到新的文件中,这样就不会对原文件造成影响。


这种情况在处理本地图片的压缩中非常常见,希望能够给大家一点启示吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值