Retriving图像从服务器到Android应用程序 [英] Retriving image from server to android app

查看:181
本文介绍了Retriving图像从服务器到Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包com.sample.downloadImage;
进口java.io.BufferedInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口java.net.URLConnection中;
进口org.apache.http.util.ByteArrayBuffer;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.widget.ImageView;

公共类downloadImage延伸活动{

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

  点阵位图= DownloadImage(http://www.allindiaflorist.com/imgs/arrangemen4.jpg);


        ImageView的IMG =(ImageView的)findViewById(R.id.img);
        img.setImageBitmap(位);
    }

    私人的InputStream OpenHttpConnection(字符串urlString)
    抛出IOException异常
    {
        在的InputStream = NULL;
        INT响应= -1;

        网址URL =新的URL(urlString);
        URLConnection的康恩= url.openConnection();

        如果(!(康涅狄格州的instanceof的HttpURLConnection))
            抛出新的IOException异常(不是一个HTTP连接);

        尝试{
            HttpURLConnection的httpConn =(HttpURLConnection类),康涅狄格州;
            httpConn.setAllowUserInteraction(假);
            httpConn.setInstanceFollowRedirects(真正的);
            httpConn.setRequestMethod(GET);
            httpConn.connect();
            响应= httpConn.getResponse code();
            如果(响应== HttpURLConnection.HTTP_OK){
                在= httpConn.getInputStream();
            }
        }
        赶上(例外前)
        {
            抛出新的IOException异常(错误连接);
        }
        返回;
    }
    私人位图DownloadImage(字符串URL)
    {
        点阵位图= NULL;
        在的InputStream = NULL;



        尝试 {
            在= OpenHttpConnection(URL);
            的BufferedInputStream双=新的BufferedInputStream(中,8190);

            ByteArrayBuffer BAF =新ByteArrayBuffer(50);
            INT电流= 0;
            而((电流= bis.read())!=  -  1)
            {
                baf.append((字节)电流);
            }
            byte []的为imageData = baf.toByteArray();
            位= BitmapFactory.de codeByteArray(为imageData,0,imageData.length);
            附寄();
        }
       赶上(IOException异常E1)
       {

            e1.printStackTrace();
        }
        返回的位图;
    }
}
 

想retrive从服务器映像,所以我试图张贴图像服务器和retrive通过URL但它非常适用于小型图像,当谈到对原图超过60KB,可能有些人给一个思路来解决问题

 包com.sample.downloadImage;
进口java.io.BufferedInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口java.net.URLConnection中;
进口org.apache.http.util.ByteArrayBuffer;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.widget.ImageView;

公共类downloadImage延伸活动{

    HttpURLConnection的httpConn;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

  点阵位图= DownloadImage(http://www.allindiaflorist.com/imgs/arrangemen4.jpg);


        ImageView的IMG =(ImageView的)findViewById(R.id.img);
        img.setImageBitmap(位);
    }

    私人的InputStream OpenHttpConnection(字符串urlString)
    抛出IOException异常
    {
        在的InputStream = NULL;
        INT响应= -1;

        网址URL =新的URL(urlString);
        URLConnection的康恩= url.openConnection();

        如果(!(康涅狄格州的instanceof的HttpURLConnection))
            抛出新的IOException异常(不是一个HTTP连接);

        尝试{
             httpConn =(HttpURLConnection类),康涅狄格州;
            httpConn.setAllowUserInteraction(假);
            httpConn.setInstanceFollowRedirects(真正的);
            httpConn.setRequestMethod(GET);
            httpConn.connect();
            响应= httpConn.getResponse code();
            如果(响应== HttpURLConnection.HTTP_OK){
                在= httpConn.getInputStream();

                DownloadImage(urlString);
            }
        }
        赶上(例外前)
        {
            抛出新的IOException异常(错误连接);
        }
        返回;
    }

    私人位图DownloadImage(字符串URL)
    {
        点阵位图= NULL;
        // InputStream的是= NULL;
        在的InputStream;
        尝试
        {
            在= httpConn.getInputStream();
            的BufferedInputStream双=新的BufferedInputStream(在3 * 1024);
            ByteArrayBuffer BAF =新ByteArrayBuffer(50);
            INT电流= 0;
            而((电流= bis.read())!=  -  1)
            {
                baf.append((字节)电流);
                byte []的为imageData = baf.toByteArray();
                位= BitmapFactory.de codeByteArray(为imageData,0,imageData.length);
                返回的位图;
            }
        }
        赶上(IOException异常E)
        {
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        返回的位图;
          }
}
 

解决方案

格式:的加载大图从服务器上的Andr​​oid

它的情况并不少见BitmapFactory.de codeFromStream()放弃了,只是返回null时将其直接连接的远程连接到的InputStream。在内部,如果你没有提供的BufferedInputStream的方法,将包装提供的流于一体的具有16384一种选择,有时工作原理是通过一个较大的缓冲区大小的BufferedInputStream缓冲区大小,如:

的BufferedInputStream双=新的BufferedInputStream(是,32 * 1024); 更普遍有效的方法是要下载的文件完全第一,然后去$ C C这样的数据$:

 的InputStream是= connection.getInputStream();
的BufferedInputStream双=新的BufferedInputStream(是,8190);

ByteArrayBuffer BAF =新ByteArrayBuffer(50);
INT电流= 0;
而((电流= bis.read())!=  -  1){
    baf.append((字节)电流);
}
byte []的为imageData = baf.toByteArray();
BitmapFactory.de codeByteArray(为imageData,0,imageData.length);
 

仅供参考,缓冲区大小在这个例子有些武断。正如在其他的答案已经说了,这是一个奇妙的想法不是保持图像的内存大小的时间比你不得不这样做。你可能会考虑直接写入到一个文件,并显示下采样版本。

希望帮助!

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

wanna retrive images from server , so i tried to post a image in server and retrive through url but it works good for small images and when it comes for big image more than 60kb , could some one give a idea to solve the problem

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

解决方案

form : Load Large Image from server on Android

it is not uncommon for BitmapFactory.decodeFromStream() to give up and just return null when you connect it directly to the InputStream of a remote connection. Internally, if you did not provide a BufferedInputStream to the method, it will wrap the supplied stream in one with a buffer size of 16384. One option that sometimes works is to pass a BufferedInputStream with a larger buffer size like:

BufferedInputStream bis = new BufferedInputStream(is, 32 * 1024); A more universally effective method is to download the file completely first, and then decode the data like this:

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

FYI, the buffer sizes in this example are somewhat arbitrary. As has been said in other answers, it's a fantastic idea not to keep an image that size in memory longer than you have to. You might consider writing it directly to a file and displaying a downsampled version.

Hope that helps!

这篇关于Retriving图像从服务器到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆