私は連絡先コンテンツプロバイダーのようなデータベースを持っています。ユーザーは各連絡先の画像をキャプチャできます。キャプチャした後、画像をbase64にエンコードしてファイルに保存し、その画像フィールドをファイルのパスで更新し、すべてを同期します。ユーザーがオンラインの場合はサーバーへの連絡先、および必要に応じてサーバーからすべてのデータを取得していますが、ファイルから画像をフェッチしているときに、メモリ不足の例外base64に直面しています。データベースに画像を保存すると、問題を解決しますか?
1313 次
1 に答える
1
通常、画像全体をエンコードしようとすると、特に Android で OutOfMemoryException が発生します。そのために、画像データをチャンクで読み取り、チャンクにエンコードを適用した後、チャンクを一時ファイルに保存します。エンコードが完了したら、エンコードされた画像ファイルでやりたいことを何でもしてください。
ファイルから画像をエンコードし、チャンクを使用してファイルに保存するコードは次のとおりです。
String imagePath = "Your Image Path";
String encodedImagePath = "Path For New Encoded File";
InputStream aInput;
Base64OutputStream imageOut = null;
try {
aInput = new FileInputStream(imagePath);
// carries the data from input to output :
byte[] bucket = new byte[4 * 1024];
FileOutputStream result = new FileOutputStream(encodedImagePath);
imageOut = new Base64OutputStream(result, Base64.NO_WRAP);
int bytesRead = 0;
while (bytesRead != -1) {
// aInput.read() returns -1, 0, or more :
bytesRead = aInput.read(bucket);
if (bytesRead > 0) {
imageOut.write(bucket, 0, bytesRead);
imageOut.flush();
}
imageOut.flush();
imageOut.close();
} catch (Exception ex) {
Log.e(">>", "error", ex);
}
于 2013-01-08T15:31:31.640 に答える