標籤: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 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;}
我們是先取出原檔案,對他進行特定的處理,例片的話,可以進行壓縮什麼的,然後將處理之後的圖片存放到新的檔案中,這樣就不會對原檔案造成影響。
這種情況在處理本地圖片的壓縮中非常常見,希望能夠給大家一點啟示吧。
android把記憶卡中的圖片或者其他的檔案轉存在其他的路徑中