package com.sample.downloadImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class downloadImage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
BufferedInputStream bis = new BufferedInputStream(in, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
bitmap =BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
in.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
return bitmap;
}
}
想要从服务器中检索图像,所以我试图在服务器中发布图像并通过网址进行检索但是它适用于小图像,当它出现超过60kb的大图像时,是否有人可以提出解决问题的想法
package com.sample.downloadImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class downloadImage extends Activity {
HttpURLConnection httpConn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
DownloadImage(urlString);
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
//InputStream is = null;
InputStream in;
try
{
in = httpConn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(in, 3 *1024);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte)current);
byte[] imageData = baf.toByteArray();
bitmap =BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
return bitmap;
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
}
答案 0 :(得分:3)
表格:Load Large Image from server on Android
BitmapFactory.decodeFromStream()放弃并且直接将其直接连接到远程连接的InputStream时返回null并不罕见。在内部,如果你没有为方法提供BufferedInputStream,它会将提供的流包装在一个缓冲区大小为16384的流中。有时可行的一个选项是传递缓冲区大小更大的BufferedInputStream:
BufferedInputStream bis = new BufferedInputStream(is,32 * 1024); 一种更普遍有效的方法是首先完全下载文件,然后像这样解码数据:
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
仅供参考,本例中的缓冲区大小有些随意。正如在其他答案中所说的那样,不要将图像在内存中保留的时间长于你所需要的,这是一个奇妙的想法。您可以考虑将其直接写入文件并显示缩减采样版本。
希望有所帮助!
答案 1 :(得分:2)
这也可以帮到你
http://blog.sptechnolab.com/2011/03/04/android/android-load-image-from-url/
http://developer.android.com/training/displaying-bitmaps/index.html
答案 2 :(得分:0)
查看此页面并下载示例代码。它会解决你的问题
http://developer.android.com/training/displaying-bitmaps/index.html