android把記憶卡中的圖片或者其他的檔案轉存在其他的路徑中

來源:互聯網
上載者:User

標籤: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把記憶卡中的圖片或者其他的檔案轉存在其他的路徑中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.