android - 从服务器检索图像到 Android 应用程序

标签 android android-layout android-service android-image

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;  
          }
}

最佳答案

表格:Load Large Image from server on Android

当您直接将 BitmapFactory.decodeFromStream() 连接到远程连接的 InputStream 时,BitmapFactory.decodeFromStream() 放弃并仅返回 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);

仅供引用,此示例中的缓冲区大小有些任意。正如其他答案中所说,不要将这么大的图像在内存中保留的时间比您需要的时间长,这是一个很棒的主意。您可以考虑将其直接写入文件并显示下采样版本。

希望有帮助!

关于android - 从服务器检索图像到 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10550349/

相关文章:

Android Activity 中的多个 YouTube 视频

android - Java 构建路径错误

android - 透明小部件背景?

android - 服务启动的 Intent 过滤器

android - 从服务内部检测系统重新启动

android - 字符串转 double 方法

android - 如何使用任何 "not Activity"中的铃声选择器(无 Activity)

android - 如何实现注销功能

android - 为什么我的文字总是位于屏幕顶部而不是中心?

android - 为什么要使用 Service 而不是 IntentService?