這裏是一種將位圖保存到存儲的方法。 imageMainDirectory看起來像「/ MyApp /」。該imageDirectory看起來像「/ MyApp的/ MyImages /」
public boolean saveBitmapToStorage(Bitmap bitmap, String imageMainDirectory,
String imageDirectory, String fileName) {
boolean result = false;
// This is true if the saving of image to the external storage fails.
// Then we proceed to the internal storage
boolean proceedToInternal;
// You need to check if External Storage is writable/readable
if (MemoryManager.isExternalStorageWritable()
&& MemoryManager.isExternalStorageReadable()) {
// Check if Memory is equals or greater than 10mb
if (MemoryManager.checkSdCardMemory(10)) {
File imgdir = new File(Environment.getExternalStorageDirectory()
+ imageDirectory);
File file = new File(imgdir, fileName);
// (Optional) replace file if its already existing
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
out.flush();
out.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
proceedToInternal = false;
} else {
proceedToInternal = true;
}
} else {
proceedToInternal = true;
}
if (proceedToInternal) {
if (MemoryManager.checkInternalMemory(10)) {
File imgdir = new File(Environment.getDataDirectory()
+ imageDirectory);
File file = new File(imgdir, fileName);
// (Optional) replace file if its already existing
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
out.flush();
out.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return result;
}
這裏是內存管理器類:
public class MemoryManager {
public static boolean checkSdCardMemory(long desiredMemory) {
boolean result = true;
StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
long bytesAvailable = (long) stat.getBlockSize()
* (long) stat.getAvailableBlocks();
long megAvailable = bytesAvailable/1048576;
if (megAvailable < desiredMemory) {
result = false;
}
return result;
}
public static boolean checkInternalMemory(long desiredMemory) {
boolean result = true;
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long) stat.getBlockSize()
* (long) stat.getAvailableBlocks();
long megAvailable = bytesAvailable/1048576;
if (megAvailable < desiredMemory) {
result = false;
}
return result;
}
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
}
謝謝... shouldni插入所有的代碼在我的主類 – shwettha
isExternalStorageReadable().createDIRNoMediaFile cnnot解決方法:錯誤 – shwettha
對不起,我刪除了未使用的行。請參閱編輯。 – icaneatclouds