URLをビットマップ画像に変換したいのですが、このビットマップを圧縮してSDカードに保存しようとしていますが、コードを実行すると、Web画像ビューに画像が表示されません。私は次のコードを持っています:
Logger.d(LOG_TAG, "Enter retrieveImageData()");
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(CONNECTTION_TIMEOUT);
// determine the image size and allocate a buffer
int fileSize = connection.getContentLength();
if (fileSize < 0) {
Logger.d(LOG_TAG, "retrieveImageData()->file size less than 0");
return null;
}
byte[] imageData = null;
byte[] buffer = new byte[BUFFER_SIZE];
// download the file
// if(Global.show_logs) Log.d(LOG_TAG, "fetching image " + imageUrl +
// " (" + fileSize + ")");
BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
if ((!(height == -1)) && (!(width == -1))) {
Logger.d(LOG_TAG, "Enter retrieveImageData() :width="+width+" height"+height);
File tmpFile = GlobalFunctions.getTmpFile();
if (tmpFile == null)
throw new IOException("DroidFu::ImageLoader: Could not create temp file!");
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(tmpFile));
Logger.d(LOG_TAG, "before call of IOUtils.copy");
IOUtils.copy(istream, ostream);
Logger.d(LOG_TAG, "after call of IOUtils.copy");
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(tmpFile.getAbsolutePath(), options);
// Calculate inSampleSize
Logger.d(LOG_TAG, "before call of calculateInSampleSize()");
options.inSampleSize = calculateInSampleSize(options, width, height);
Logger.d(LOG_TAG, "after call of calculateInSampleSize()");
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Logger.d(LOG_TAG, "Absolute path of tmp file is: "+tmpFile.getAbsolutePath());
Bitmap b=BitmapFactory.decodeFile(tmpFile.getAbsolutePath(), options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
b.recycle();
//imageData = stream.toByteArray();
istream.close();
ostream.close();
stream.close();
Logger.d(LOG_TAG, "Exit retrieveImageData() after resizing to imageview");
return byteArray;
}
ただし、このコードはこの行で例外(Throwable e)をスローし、「e」もnullです。
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();