我的應用程序允許用戶使用相機拍攝照片並將其保存爲他們的個人資料照片。一次只能存儲一個圖像?Android - 在設備上存儲圖像?
- 爲此目的使用SharedPrefertences是一個壞主意,雖然我只存儲1個圖像? (將圖像轉換爲Base64)。有什麼缺點?
- 如果使用共享偏好來存儲圖像不是一個好主意,有什麼選擇?
我的應用程序允許用戶使用相機拍攝照片並將其保存爲他們的個人資料照片。一次只能存儲一個圖像?Android - 在設備上存儲圖像?
我認爲在SharedPreferences中存儲二進制數據不是一個好主意。而是將其保存到文件系統。例如,如果數據來自InputStream:
storeImage(new File(context.getFilesDir().getAbsolutePath() + fileDir),
is,
"profile.png");
public static void storeImage(
File fileDir,
InputStream inputStream,
String fileName) throws IOException {
File file = new File(fileDir,fileName);
FileOutputStream fos = new FileOutputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
inputStream.close();
fos.close();
bitmap.recycle();
bitmap = null;
}
其中上下文可以是應用程序/活動上下文。
[apply](http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply())提供的簡單後臺工作如何? 「您不必擔心Android組件生命週期以及與apply()寫入磁盤的交互,該框架確保在切換狀態之前從apply()完成正在進行的磁盤寫入。」當你進入文件系統級別時,你可以阻止你的主線程處理線程。 –