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

来源:互联网 发布:实时数据库 编辑:程序博客网 时间:2024/04/28 12:03

这是一个比较简单的问题,但是也是我们经常回遇到的问题,就是在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 inSampleSizeBitmapFactory.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