将图像从服务器检索到android应用程序
想要从服务器上检索图像,所以我尝试在服务器上发布一个图像,并通过url检索,但它对小图像效果很好,当涉及到超过60kb的大图像时,有人能给出解决问题的方法吗将图像从服务器检索到android应用程序,android,android-layout,android-service,android-image,Android,Android Layout,Android Service,Android Image,想要从服务器上检索图像,所以我尝试在服务器上发布一个图像,并通过url检索,但它对小图像效果很好,当涉及到超过60kb的大图像时,有人能给出解决问题的方法吗 package com.sample.downloadImage; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import jav
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;
}
}
表格:
BitmapFactory.decodeFromStream()放弃并直接将其连接到远程连接的InputStream时返回null并不少见。在内部,如果您没有向该方法提供BufferedInputStream,它将把提供的流包装成一个缓冲区大小为16384的流。有时有效的一个选项是传递缓冲区大小更大的BufferedInputStream,如:
BufferedInputStream bis=新的BufferedInputStream(is,32*1024);
一种更普遍有效的方法是先完全下载文件,然后像这样解码数据:
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;
}
}
仅供参考,本例中的缓冲区大小有些随意。正如在其他答案中所说的那样,不在内存中保存这样大小的图像是一个非常好的主意。您可能会考虑将其直接写入文件并显示下采样的版本。
希望有帮助 查看此页面并下载示例代码。它会解决你的问题
这也可能对您有所帮助
1-。2-InputStream is=connection.getInputStream();上面使用的连接是指HttpURLConnection的对象或UrlConnection的对象。当我进行调试时,“InputStream is=connection.getInputStream();”我应该在这一部分中传递url,您能详细告诉我是什么错误吗?如果可能,请提供相同的日志cat.05-15 01:19:13.104:D/AndroidRuntime(542):关闭VM 05-15 01:19:13.104:W/dalvikvm(542):threadid=1:线程退出时出现未捕获异常(组=0x4001d800)05-15 01:19:13.123:E/AndroidRuntime(542):致命异常:main 05-15 01:19:13.123:E/AndroidRuntime(542):java.lang.RuntimeException:无法启动活动组件信息{com.sample.downloadImage/com.sample.downloadImage.downloadImage}:java.lang.NullPointerException 05-15 01:19:13.123:E/AndroidRuntime(542):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)你能给我一些其他可能的链接来了解如何在安全方式下加载大尺寸图像吗?我已经尝试了上面的链接,这些链接支持小图像,但是我被一些大尺寸图像挡住了
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);