将图像下载到 Android 应用程序的 coverflow 时出现内存不足错误
通过网络服务,我将每个图像写入 JPG 文件(在 sdcard-“mnt/sdcard/img.jpg”中)并将图像显示到 coverflow 视图中。我正在获取第一个图像,但是当涉及到第二个图像时和其他图像我出现内存不足错误。
下面我给出了从 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;
}
}
}
Through web service i am writing each image in to a JPG file(in sdcard- "mnt/sdcard/img.jpg") and showing the images in to a coverflow view.i am getting the first image but when it comes to the second and other images i am getting outofmemoryerrorr.
below i have given the code to get the image from 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论