标签:des android blog io os ar java for 文件
这是一个比较简单的问题,但是也是我们经常回遇到的问题,就是在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;
}
这种情况在处理本地图片的压缩中非常常见,希望能够给大家一点启示吧。
android把内存卡中的图片或者其他的文件转存在其他的路径中
标签:des android blog io os ar java for 文件
原文地址:http://blog.csdn.net/litianpenghaha/article/details/40586101