通过网络服务,我将每个图像写入JPG文件(在sdcard-“mnt / sdcard / img.jpg”中)并将图像显示到封面视图中。我正在获取第一张图片但是当它到来时对于第二张和其他图像我正在超越记忆。
下面我已经给出了从url获取图像的代码
public Bitmap getImageDirectHit(String directHitUrl){
Bitmap bmImg;
String fileUrl=directHitUrl;
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bmImg = null;
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
//int length = conn.getContentLength();
InputStream is = conn.getInputStream();
//conn.disconnect();
// BufferedInputStream bis = new BufferedInputStream(is);
// ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);
// int current = 0;
// while ((current = bis.read()) != -1) {
// byteArrayBuffer.append((byte) current);
// }
//
File nf = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
FileOutputStream fos = new FileOutputStream(nf);
//byte[] buf = new byte[2048];
//int n;
// fos.write(bitmapdata, 0, bitmapdata.length);
byte[] bufu=new byte[20];
int len = 0;
while((len = is.read(bufu))>0) {
fos.write(bufu);
}
fos.close();
fos = null;
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// int next = is.read();
// while (next > -1) {
// bos.write(next);
// next = is.read();
// }
// bos.flush();
// byte[] result = bos.toByteArray();
// bos.close();
// bos = null;
Bitmap tempBitmap =
BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
//Bitmap tempBitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
//result = null;
bmImg = Bitmap.createScaledBitmap(tempBitmap, 150, 180, true);
tempBitmap.recycle();
tempBitmap = null;
// bmImg = BitmapFactory.decodeStream(is);
// Log.i("Byte Array Size", ":"+byteArrayBuffer.length());
// bis.close();
is.close();
// bis = null;
is = null;
return bmImg;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}