android 存儲圖片到data目錄和讀取data目錄下的圖片



public void storePic(String tabid, String key, Bitmap bitmap) {
LogUtils.LOGD(TAG, "storePic begin tabid = " + tabid + "key = " + key);
if(tabid == null || key == null || tabid.isEmpty() || key.isEmpty() || bitmap == null) {
return;
}
FileOutputStream fos = null;
try {
fos = getActivity().openFileOutput(tabid + "_" + key, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
LogUtils.LOGE(TAG, "storePic FileNotFoundException e = " +e);
} finally {
if(fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
LogUtils.LOGE(TAG, "storePic IOException e = " +e);
}
}
}
}

public Bitmap getStorePic(String tabid, String key) {
LogUtils.LOGD(TAG, "getStorePic begin tabid = " + tabid + "key = " + key);
if(tabid == null || key == null || tabid.isEmpty() || key.isEmpty()) {
return null;
}
FileInputStream fin = null;
Bitmap bitmap = null;
try {
fin = getActivity().openFileInput(tabid + "_" + key);
bitmap = BitmapFactory.decodeStream(fin);
} catch (FileNotFoundException e) {
LogUtils.LOGE(TAG, "getStorePic FileNotFoundException e = " + e);
}
return bitmap;
}


總而流程:
存儲圖片代碼:

[java]  view plain  copy
  1. String str1 = "icon.png";  
  2.   
  3. FileOutputStream localFileOutputStream1 = openFileOutput(str1, 0);  
  4.   
  5. Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.PNG;  
  6.   
  7. bitmap.compress(localCompressFormat, 100, localFileOutputStream1);  
  8.   
  9. localFileOutputStream1.close();  

讀取圖片代碼:

[java]  view plain  copy
  1. String localIconNormal = "icon.png";  
  2.   
  3. FileInputStream localStream = openFileInput(localIconNormal);  
  4.   
  5. Bitmap bitmap = BitmapFactory.decodeStream(localStream));  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM