Java Android:应用程序在加载许多图像时崩溃
我正在android应用程序中加载一本书作为图像 当用户按下下一页键时,应用程序将下载一个新图像(在asynctask中),并在我的imageview中设置我的新图像 但是8页之后…:| (并且我没有在变量中保存以前的页面) 更新: 代码:Java Android:应用程序在加载许多图像时崩溃,java,android,android-asynctask,Java,Android,Android Asynctask,我正在android应用程序中加载一本书作为图像 当用户按下下一页键时,应用程序将下载一个新图像(在asynctask中),并在我的imageview中设置我的新图像 但是8页之后…:| (并且我没有在变量中保存以前的页面) 更新: 代码: public byte[] getUrlImgContent(String urlstring) throws IOException { byte[] imageRaw = null; URL url = new URL(urlstring
public byte[] getUrlImgContent(String urlstring) throws IOException
{
byte[] imageRaw = null;
URL url = new URL(urlstring);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setUseCaches(false);
urlConnection.connect();
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
try
{
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
out.flush();
imageRaw = out.toByteArray();
urlConnection.disconnect();
in.close();
out.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageRaw;
}
return null;
}
这是显而易见的。试试图像大小。什么?!但是为什么呢。它只加载了一个图像!:|你应该发布你的代码,这样我们可以帮助你。很可能您没有使用
BitmapOptions
对象来指定下载的大小。现在更新了下载方法:DYou应该从开头开始,例如官方开发人员网站上的培训部分。你会发现:谷歌搜索也是个好主意,你会发现很多类似的问题。。。
public byte[] getUrlImgContent(String urlstring) throws IOException
{
byte[] imageRaw = null;
URL url = new URL(urlstring);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setUseCaches(false);
urlConnection.connect();
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
try
{
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
out.flush();
imageRaw = out.toByteArray();
urlConnection.disconnect();
in.close();
out.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageRaw;
}
return null;
}